The previous application of command in OSGi
In the previous blog post, we explained the blueprint in OSGi, but did not make any specific use of this, and in this article and later will talk about other applications in OSGi blueprint in the time to explain, this article will talk about the command in OSGi, In the written application, may involve data migration or some other operations, if this by invoking the interface to operate, if the non-related people get to the relevant interface call way, it may bring some harm, so in the last resort, the interface will not use the way, The role of command at this time is manifested, if you write a number of commands, and then run the command in the container to execute commands, you can directly do what we need to do, and then we start using the relevant API to complete our command.
Related Programs
The command using OSGi must implement the action interface in the related class, which is a interface provided by Felix, specifically
import org.apache.felix.gogo.commands.Action
In its implementation of the method to write the program logic we need, we command to execute the content is here, in which to mention is not just can implement the action interface One, can also inherit the Osgicommandsupport class, the implementation method Doexecute, You can also achieve the same effect, where the Osgicommandsupport package name and class name are as follows:
import org.apache.karaf.shell.console.OsgiCommandSupport
In fact, there is a kind of:
org.apache.karaf.shell.api.action.Action
This should also be possible, but in the use of the time, Karaf will error, so this again for the moment, and so on, and then have the opportunity to figure out again this, I estimate, now the best to achieve is this one, because the first two have been deprecated.
After implementing the appropriate method, you also need to add the following annotations to the class:
@Service@Log4j@Command(scope"test""hello""start for hello world")
@Log4j annotations are optional, in this is mainly used to record the log, plus @service and @command annotations are required, labeled as command and used in the Karaf console, the specific parameters used to see the corresponding annotations of the source code, which have detailed comments and instructions.
Commmand Basic is so many things, including the source code:
Importlombok.extern.log4j.Log4j;ImportOrg.apache.felix.gogo.commands.Action;ImportOrg.apache.felix.service.command.CommandSession;ImportOrg.apache.karaf.shell.api.action.Command;ImportOrg.apache.karaf.shell.api.action.lifecycle.Service;/** * Created by Xiaxuan on 16/4/20. * *@Service@Log4j@Command(Scope ="Test", name ="Hello", Description ="Start for Hello World") Public class Hellocommand implements Action{ PublicObjectExecute(Commandsession session)throwsException {Log.info ("Hello word");return NULL; }}
After this is done, you also need to configure the command in the Blueprint.xml file as follows:
<command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.0.0"> <command name="test/hello"> <action class="cn.com.files.command.HelloCommand"></action> </command> </command-bundle>
Where command's name is the corresponding scope and name in our @command annotations, after that we can start the Karaf container and start using our command.
Program run
After starting the container, on the command line, open the Karaf client, for the following:
First enter the list command to ensure that the bundle starts normally, and this example is written in the bundle of storage, as follows:
The status of the bundle with ID 13 is active, we can determine that our bundle is active, now the console input test, for our previous command scope, press the TAB key, prompt as follows:
There are a number of hints, the first one can be ignored, the second one is for us to write command commands, the third is what I will tell later.
Console input Test:hello, view console output:
The console gets the normal output, Hello World, and the command executes normally.
Usage specifications and other
Personal use of command, more is used in the data import or data repair when used, normal business logic is generally not used command to achieve.
I used the command implementation of the interface or inherited classes, two are obsolete interface or abstract class, now generally used should be the karaf of the action, but there may be some problems in the use, the personal think may be in the configuration and the current difference, should not be related to annotations, this action class for Karaf is also mentioned in the comments in the @service annotations. However, this article does not use this action, hope to have used to discuss with me.
command can implement the interface or inherit the class to do, can also customize their own command, and then in the blueprint to do the relevant configuration can be a little simpler than the above command, the usage is a little more diversified.
In the above command, the command can pass in parameters, and there can be multiple parameters, which may be mentioned in the next blog post.
The command written above is run based on the Karaf container.
# using a custom command in OSGi