Home > Oracle MiddleWare, WebLogic > Adding java.util.List to JMS Queue – Sample Code

Adding java.util.List to JMS Queue – Sample Code


I was looking at a way to add java.util.ArrayList collection to existing JMS Queue & here is how you do it.

You may replace ArrayList with Map/HashMap as well.

This post uses my previous post’s JMS Queue Configuration.

Sender Code Snippet

import java.io.Serializable;
import java.util.List;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;

/**
 * This adds java.util.List to JMS Queue
 *
 * @author karun.chennuri
 *
 */
public class JMSQueueSender {

 private ObjectMessage msg;
 /**
 * Sends a message to a JMS queue.
 *
 * @param message message to be sent
 * @exception JMSException if JMS fails to send message due to internal error
 */
 public void send(List<String> listData) throws Exception {
 JMSContextSender context = JMSContextSender.getInstance(); //Get JMS Context. Replace with appropriate code to get JMSContext
 msg = context.getQueueSession().createObjectMessage(); //Get Queue Session and create Object Message

 msg.setObject((Serializable)listData);
 context.getQueueSender().send(msg);
 System.out.println("message sent...");
 }

}

Receiver Code Snippet

import java.util.ArrayList;
import java.util.List;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.QueueConnection;
import javax.jms.QueueReceiver;

/**
 *
 * Receiver Code. Receives ArrayList from Queue
 * @author karun.chennuri
 *
 */
public class JMSQueueReceiver implements MessageListener {

/**

* Message listener interface.
 *
 * @param msg
 * message
 */
 public void onMessage(Message msg) {
 try {
 List<String> listData = null;
 ObjectMessage objMessage = (ObjectMessage) msg;

listData = (ArrayList) objMessage.getObject();

System.out.println("listData: " + listData);

} catch (JMSException jmse) {
 System.err.println("An exception occurred: " + jmse.getMessage());
 }
 }

}

Advertisements
  1. Aakash
    March 9, 2016 at 4:04 AM

    Great post. It helped me solve my problem.
    Much Appreciated !!!

    Like

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: