no

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/h...

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);  
 }
}

Related

java 3803409026248995121

Post a Comment

item