How do I create a JGroups channel in JBoss 7.x from the configuration in the standalone.xml file?
문제
I am writing an OSGI service for JBoss7.x and I need to create a new JChannel for some domain related RPC. I would like to use one of the protocol stack definitions from the standalone.xml file and will want to use shared transport if I can.
I don't know where to begin to access the information or how to inject it either.
Looking at the JBoss AS code I think I probably need to get hold of a JChannelFactory instance. How would I do this in OSGI?
해결책
Not sure if it's the recommended approach but it does work.
First, get a reference to the JBoss Modular Service Container. This can be looked up in the OSGI service registry. Using Spring DM/Blueprint:
<osgi:reference id="serviceContainer" interface="org.jboss.msc.service.ServiceContainer" />
In your code use the service container to look up a given service by name:
ServiceName sn = ServiceName.of("jboss", "jgroups", "stack", "udp");
ServiceController<?> serviceController = serviceContainer.getService(sn);
if (serviceController == null) {
throw new RuntimeException("Failed to lookup service controller for jboss.jgroups.stack.udp");
}
Service<?> service = serviceController.getService();
Object obj = service.getValue();
ChannelFactory factory = (ChannelFactory) obj;
If you need to know the service names that are available then call dumpServices()
on the ServiceContainer
to get a list in the log file.
If using Maven then add a dependency upon the JBoss MSC in your pom file:
<dependency>
<groupId>org.jboss.msc</groupId>
<artifactId>jboss-msc</artifactId>
<version>1.0.2.GA</version>
</dependency>
plus any other dependencies you may need in order to access the code used by the service. In the example above there is a need for a dependency upon the org.jboss.as/jboss-as-clustering-jgroups and org.jgroups/jgroups libraries in the pom file.
You also need to make sure that these services are defined as capabilities in the standalone.xml file:
<capability name="org.jgroups"/>
<capability name="org.jboss.as.clustering.jgroups"/>