Tuesday, September 29, 2015

JMS Listener

In this article i would like to focus on the message driven bean for activemq.

Prerequisite:

  1. ActiveMq server should be running.
  2. Configure you your topic/queue in the console there 

    3. Configure your application server with activeMQ  resource adapter. e.g. Wildfly module  should         be extracted in wildfly modules directory and same should be configured inside stadalone-                   full.xml.

<subsystem xmlns="urn:jboss:domain:resource-adapters:2.0">
            <resource-adapters>
                <resource-adapter id="activemq-rar.rar">
                    <module slot="main" id="org.apache.activemq"/>
                    <transaction-support>XATransaction</transaction-support>
                    <config-property name="ServerUrl">
                        tcp://localhost:61616
                    </config-property>
                    <config-property name="UserName">
                        defaultUser
                    </config-property>
                    <config-property name="UseInboundSession">
                        false
                    </config-property>
                    <config-property name="Password">
                        defaultPassword
                    </config-property>
                    <connection-definitions>
                        <connection-definition class-name="org.apache.activemq.ra.ActiveMQManagedConnectionFactory" jndi-name="java:/ConnectionFactory" enabled="true" pool-name="ConnectionFactory">
                            <xa-pool>
                                <min-pool-size>1</min-pool-size>
                                <max-pool-size>20</max-pool-size>
                                <prefill>false</prefill>
                                <is-same-rm-override>false</is-same-rm-override>
                            </xa-pool>
                        </connection-definition>
                    </connection-definitions>
                    <admin-objects>
                        <admin-object class-name="org.apache.activemq.command.ActiveMQTopic" jndi-name="java:jboss/activemq/topic/yourtopic" use-java-context="true" pool-name="hsn18cmstopic">
                            <config-property name="PhysicalName">
                                activemq/topic/yourtopic
                            </config-property>
                        </admin-object>
                    </admin-objects>
                </resource-adapter>
            </resource-adapters>
        </subsystem>



To write your implementation of JMS listener your class should implement the interface MessageListener(javax.jms.MessageListener).

public class YourListener implements MessageListener {


}


You need to specify the activation config parameter inside 'MessageDriven' annotation as follows.

@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "suborderupdates"),
@ActivationConfigProperty(propertyName = "useJNDI", propertyValue = "false") })

Override the onMessage method and try to extract the TextMessage from the message by parsing JSON string.


       @Override
public void onMessage(Message message) {

try {
logger.info("event received");
TextMessage textMsg = (TextMessage) message;
textMsg.getJMSType();
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonElement jelement = parser.parse(textMsg.getText())
.getAsJsonObject();
<T> omsEvent = gson.fromJson(jelement,
<T>.class);
//TODO write your code here
} catch (Exception exception) {
exception.printStackTrace();
}
.
Hence the complete class should be something like

/**
 * 
 */


import java.util.logging.Logger;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.inject.Inject;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

/**
 * @author Girish.Yadav
 *
 */
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "suborderupdates"),
@ActivationConfigProperty(propertyName = "useJNDI", propertyValue = "false") })

public class OMSListener implements MessageListener {

private static Logger logger = Logger
.getLogger(OMSListener.class.getName());

@Override
public void onMessage(Message message) {

try {
logger.info("OMS event received");
TextMessage textMsg = (TextMessage) message;
textMsg.getJMSType();
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonElement jelement = parser.parse(textMsg.getText())
.getAsJsonObject();
<T> omsEvent = gson.fromJson(jelement,
<T>.class);
//TODO write your code here

} catch (Exception exception) {
exception.printStackTrace();
}

}
}

No comments:

Post a Comment