How to receive an asynchronous jms message

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

Also, you might want to check the synchronous way of receiving jms message:

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 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") })
public class IpielGatewayMDB implements MessageListener {
 private Logger log;

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

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

 private void processAction(MessageDTO msg) throws Exception {
  log.debug("[ipiel-pg] processAction: afterDeserialize. messageDTO={}",
How to receive an asynchronous jms message How to receive an asynchronous jms message Reviewed by Edward Legaspi on Monday, June 25, 2012 Rating: 5

No comments:

Powered by Blogger.