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...
https://www.czetsuyatech.com/2012/06/javaee-receiving-asynchronous-jms-message.html
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:
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);
}
}




Post a Comment