Business Case - From Java EE To Microservices
But I want to step back a bit first: Why should someone? I think, one of the main motivations behind such a scenario is a slow migration path. Coming down all the way from monolithic, single platform applications we want to be flexible enough to shell out individual services from those giant installations and make them available as a service. Assuming, that this is even possible and the legacy application has a decent design. Or we want to advance individual services, let's say from a technical perspective. In this particular example, we can't wait to get Java EE 7 features into our application and WebLogic is still mostly stuck on EE 6. We could do this with REST services or even WebServices, but we might want more. And this is, where the JMS specification comes in.
Oracle JMS Client Libraries in WildFly
In order to send messages between two different servers, you need to have the individual client libraries integrated into the sending end. For WebLogic this is WebLogic JMS Thin Client (wljmsclient.jar). provides Java EE and WebLogic JMS functionality using a much smaller client footprint than a WebLogic Install or Full client, and a somewhat smaller client footprint than a Thin T3 client. As a matter of fact, it contains Java EE JMS APIs and implementations which will directly collide with the ones provided by WildFly. To use them, we'll have to package them as a module and and configure a JMS Bridge in HornetQ to use exactly this. First thing is to add the new module. Change folder to wildfly-8.2.0.Final\modules\system\layers\base and create a new folder structure: custom\oracle\weblogic\main underneath it. Copy the wlthint3client.jar from the %MW_HOME%\server\lib folder here. Now you have to add a module descriptor file, module.xml:
<module xmlns="urn:jboss:module:2.0" name="custom.oracle.weblogic">
<resources>
<resource-root path="wlthint3client.jar">
<filter>
<exclude-set>
<path name="javax.ejb"/>
<path name="javax.ejb.spi"/>
<path name="javax.transaction"/>
<path name="javax.jms"/>
<path name="javax.xml"/>
<path name="javax.xml.stream"/>
</exclude-set>
</filter>
</resource-root>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="sun.jdk" export="false" services="import">
<exports>
<include-set>
<path name="sun/security/acl"/>
<path name="META-INF/services"/>
</include-set>
</exports>
</module>
<module name="com.sun.xml.bind" />
<module name="org.omg.api"/>
<module name="javax.ejb.api" export="false" />
<module name="javax.transaction.api" export="false" />
<module name="javax.jms.api" export="false" />
<module name="javax.xml.stream.api" export="false" />
<module name="org.picketbox" optional="true"/>
<module name="javax.servlet.api" optional="true"/>
<module name="org.jboss.logging" optional="true"/>
<module name="org.jboss.as.web" optional="true"/>
<module name="org.jboss.as.ejb3" optional="true"/>
<module name="org.hornetq" />
</dependencies>
</module>
This file defines all the required resources and dependencies together with the relevant excludes. If this is done, we finally need the message bridge.The HornetQ JMS Message Bridge
The function of a JMS bridge is to consume messages from a source JMS destination, and send them to a target JMS destination. Typically either the source or the target destinations are on different servers. The bridge can also be used to bridge messages from other non HornetQ JMS servers, as long as they are JMS 1.1 compliant. Open the standalone-full.xml and add the following configuration to the messaging subsystem:
<jms-bridge name="wls-bridge" module="custom.oracle.weblogic">
<source>
<connection-factory name="java:/ConnectionFactory"/>
<destination name="java:/jms/sourceQ"/>
</source>
<target>
<connection-factory name="jms/WFMessagesCF"/>
<destination name="jms/WFMessages"/>
<context>
<property key="java.naming.factory.initial"
value="weblogic.jndi.WLInitialContextFactory"/>
<property key="java.naming.provider.url"
value="t3://127.0.0.1:7001"/>
</context>
</target>
<quality-of-service>AT_MOST_ONCE</quality-of-service>
<failure-retry-interval>2000</failure-retry-interval>
<max-retries>10</max-retries>
<max-batch-size>500</max-batch-size>
<max-batch-time>500</max-batch-time>
<add-messageID-in-header>true</add-messageID-in-header>
</jms-bridge>
As you can see, it references the module directly and has a source and a target definition. The source is the WildFly local message queue which is defined in the messaging subsystem: <jms-queue name="sourceQ">
<entry name="java:/jms/sourceQ"/>
</jms-queue>
And the target is the remote queue plus connection factory, which are defined in WebLogic Server. I assume, that you know how to do that, if not, please refer to this documentation. That's pretty much it. Now we need to send a message to our local queue and this is going to be send via the bridge over to the WebLogic queue.Testing The Bridge - With Camel
Deploy a message driven bean to WebLogic (Yes, you'll have to package it as an ejb jar into an ear and all of this). This particular sample just dumps the message text out to the logger.
@MessageDriven(mappedName = "jms/WFMessages", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class LogMessageBean implements MessageListener {
private final static Logger LOGGER = Logger.getLogger(LogMessageBean.class.getName());
public LogMessageBean() {
}
@Override
public void onMessage(Message message) {
TextMessage text = (TextMessage) message;
try {
LOGGER.log(Level.INFO, text.getText());
} catch (JMSException jmxe) {
LOGGER.log(Level.SEVERE, jmxe.getMessage());
}
}
}
Now we need a producer on the WildFly server. Do do this, I am actually using the WildFly-Camel JMS integration.@Startup
@ApplicationScoped
@ContextName("jms-camel-context")
public class JMSRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
// Initial Context Lookup
Context ic = new InitialContext();
ConnectionFactory cf = (ConnectionFactory) ic.lookup("/ConnectionFactory");
// Create the JMS Component
JmsComponent component = new JmsComponent();
component.setConnectionFactory(cf);
getContext().addComponent("jms", component);
// Build A JSON Greeting
JsonObject text = Json.createObjectBuilder()
.add("Greeting", "From WildFly 8").build();
// Send a Message from timer to Queue
from("timer://sendJMSMessage?fixedRate=true&period=10000")
.transform(constant(text.toString()))
.to("jms:queue:sourceQ")
.log("JMS Message sent");
}
}
That's the whole magic. A timer sends a JSON Text message to the local queue which is bridged over to WebLogic.Some More Hints
If you want to test the WebLogic Queue without the bridge, you will have to include the wljmsclient into your project. As this isn't available in a Maven repository (AFAIK), you can simply install it locally:
mvn install:install-file -Dfile=%MW_HOME%/wlserver/server/lib/wlthint3client.jar -DgeneratePom=true -DgroupId=custom.com.oracle -DartifactId=wlthint3client -Dversion=12.1.3 -Dpackaging=jar
Another important thing is, that you will run into classloading issues on WildFly, if you try to use the custom module in any other scope than the bridge. So, pay close attention, that you don't use it somewhere else.The bridge has a comparibly large failure-retry-interval and max-retries configured. This is a workaround. If WildFly startup is too fast and the bridge tries to access the local sourceQ before the queue is actually configured, it'll lead to an exception.
Find the complete source-code in my GitHub account.