How to Receive an Asynchronous Jms Message

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

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={}",


javaee 3803409026248995121

Post a Comment Default Comments


Are you looking for freelancers in the Philippines? Get in touch.


If you like what I do, you can support this channel by buying me a coffee. I would be grateful for your contribution! Your donations will help me create more FREE online courses and learning materials for programmers and remote workers.