How to receive an asynchronous jms message
Tech-Today

How to receive an asynchronous jms message


This tutorial assumes that you have glassfish installed and followed the 2 previous tutorials:

http://czetsuya-tech.blogspot.com/2012/06/how-to-setup-set-jms-factory-and-queue.html
http://czetsuya-tech.blogspot.com/2012/06/how-to-send-jms-message-to-jms-queue.html

Also, you might want to check the synchronous way of receiving jms message:
http://czetsuya-tech.blogspot.com/2012/06/how-to-receive-synchronous-jms-message.html

In asynchronous way, we need to define a class that extends javax.jms.MessageListener. This class is bound to a JMS Queue (via mappedName property), where it will listen for message. On message receive it will call the onMessage() receive method. Refer to the class below for example:

package com.ipiel.service.message;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.inject.Inject;
import javax.inject.Named;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.ws.rs.core.UriBuilder;

import org.slf4j.Logger;

import com.ipiel.commons.dto.MessageDTO;
import com.ipiel.commons.dto.util.MessageDTOHelper;

@MessageDriven(mappedName = "jms/ipielQueue", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") })
@Named("ipielGatewayMDB")
public class IpielGatewayMDB implements MessageListener {
@Inject
private Logger log;

public void onMessage(Message msg) {
log.debug("[ipiel-pg] onMessage: afterBegin. msg=" + msg);

try {
MessageDTO messageDTO = MessageDTOHelper
.deserialize((ObjectMessage) msg);
processAction(messageDTO);
} catch (Exception e) {
log.error("[ipiel-pg] onMessage: {}. msg was : {}", e.getMessage(),
msg);
}
}

private void processAction(MessageDTO msg) throws Exception {
log.debug("[ipiel-pg] processAction: afterDeserialize. messageDTO={}",
msg);
}
}




- Do Some Initialization Work After The Web Application Initialization Process
We can achieve this by using a webListener. See code below: package com.czetsuya; import java.text.MessageFormat; import java.util.ResourceBundle; import javax.inject.Inject; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;...

- How To Implement Jms Request/response Feature In Glassfish
This tutorial will provide a sample code on how you can send request and receive reply using jms. I've used Glassfish 3.1.1, with its built in JMS Broker for testing set to LOCAL. We need 2 maven projects (1 is for request and 1 for receiving the...

- How To Load Property File From Glassfish Config's Folder
In seam you can define a component class in components.xml that will load the properties from the JBOSS_CONFIG folder. Add the ff lines: <component name="paramBean" class="com.ipil.PropertyBean" scope="application" auto-create="true" startup="true">...

- How To Receive A Synchronous Jms Message
This tutorial requires that you have glassfish installed and followed the following previous 2 tutorials: http://czetsuya-tech.blogspot.com/2012/06/how-to-setup-set-jms-factory-and-queue.html http://czetsuya-tech.blogspot.com/2012/06/how-to-send-jms-message-to-jms-queue.html...

- How To Setup Set Jms Factory And Queue In Glassfish
This tutorial assumes that you already have glassfish installed and running on your local machine. 1.) To check the default setting of your JMS broker navigate to Configurations->server-config->Java Message Service->JMS Hosts->default_JMS_host....



Tech-Today








.