Akin Dönmez
Akin Dönmez

Reputation: 363

JMS how to get mulitple messages with JMSMessageListener

I m trying to sent to a consumer a message from client. Consumer expects many message types like Booking or Confirmation. So when i get the message i m trying to see if the message is instance of Booking or Confirmation. So far the instance of is not working, i cant see if the object is Booking or Confirmation. Besides my JMSMessageListener doesnt seem to be the best way of doing the scenario. The scenario is : Client sends a Booking to agent, agent forwards the message to different consumers, agent gets confirmation from consumers and sends a confirmation back. Any ideas why instance of not working and for this scenario, is this a good way to implement a JMSMessageListener to an agent for multiple purposes ?

//JMS Message Listener
public class JMSAgentMessageListener implements MessageListener {

    private ConnectionFactory factory = null;
    private Connection connection = null;
    private Session session = null;
    private  Destination BookingQueue = null;
    private  Destination consolidatorQueue1 = null;
    private  Destination consolidatorQueue2 = null;
    private MessageConsumer consumer = null;
    private Destination agentConfirmQueue = null;
    private static MessageProducer producer = null;
    final static Logger logger = Logger.getLogger(TravelAgent.class);
    private  Destination customerConfirmQueue = null;

    @Override
    public void onMessage(Message message) {
        try {
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "tcp://localhost:61616");
            // Create a Connection
            Connection connection = connectionFactory.createConnection();
            session = connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);

            if (message instanceof Booking) {

                Booking booking = (Booking) message;
                logger.info("# Received order for " + booking.getCustomer());
                customerConfirmQueue = message.getJMSReplyTo();

                logger.info("# The travel agent forwards booking orders to the airfare consolidators");
                 ObjectMessage messageToConsilator1 = session . createObjectMessage ( booking ) ;
                 agentConfirmQueue = session.createQueue("AgentConfirmQueue");
                 consolidatorQueue1 = session.createQueue("ConsolidatorQueue1");
                 messageToConsilator1.setJMSReplyTo(agentConfirmQueue);
                 messageToConsilator1.setJMSDestination(consolidatorQueue1);

                producer = session.createProducer(consolidatorQueue1);
                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                producer.send(messageToConsilator1);

                }else if(message instanceof Confirmation){
                    logger.info("# The travel agent recieved booking orders to the airfare consolidators");
                    Confirmation confirmation = (Confirmation) message;
                    logger.info(confirmation.getMessage()+"received");
                    logger.info("# The travel agent notfiying the customers");

                     ObjectMessage messageToClient = session . createObjectMessage ( confirmation ) ;
                     customerConfirmQueue = session.createQueue("customerConfirmQueue");

                    producer = session.createProducer(customerConfirmQueue);
                    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                    producer.send(messageToClient);
                }
                producer.close();
                session.close();
                connection.close();

        } catch (JMSException e) {
            logger.error("couldnt get the message");
        }


    }
}

Upvotes: 0

Views: 46

Answers (1)

klog
klog

Reputation: 486

Message isn't in the hierarchy of your class inheritance tree. JMS offers, different types of messages that can be disseminated, Text, Object, etc., all inheriting from Message. It is these types that you could call instance of on. Once Message is cast to an ObjectMessage, you can then call getObject() and then cast it/check instanceof for your types.

I'd recommend limiting types of messages that can be sent on a topic as it lends itself to some messier code. I try to avoid using instanceof if at all possible by refactoring the code in a way that makes it unnecessary. To reduce the need for instanceof, I'd create a topic per type of information being conveyed or develop your classes in such a way that they inherit from the same parent or implement the same interface.

Upvotes: 1

Related Questions