Sample JMS Queue program

Try this after setting up the environment for JMS. The programs are purely for Jboss environment.
SimpleQueueSender

Queue Receiver

package com.stratify.apps.cprt.frontend.crawler.Queue;

import java.io.IOException;
import java.util.Properties;

import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class QueueMessageSender {
	public static void main(String[] args) throws IOException {
		String queueName = null;
		Context jndiContext = null;
		QueueConnectionFactory queueConnectionFactory = null;
		QueueConnection queueConnection = null;
		QueueSession queueSession = null;
		Queue queue = null;
		QueueSender queueSender = null;
		TextMessage message = null;

		try {
			Properties p = new Properties();

			p.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");
			p.put(Context.PROVIDER_URL, "jnp://192.168.192.109:1099");
			p.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
			// Context ctx=new InitialContext(p);
			jndiContext = new InitialContext(p);
		} catch (NamingException e) {
			System.out.println("Could not create JNDI API " + "context: "
					+ e.toString());
			System.exit(1);
		}
		try {
			queueConnectionFactory = (QueueConnectionFactory) jndiContext
					.lookup("ConnectionFactory");
			queue = (Queue) jndiContext.lookup("queue/IncomingMessageQueue");
		} catch (NamingException e) {
			System.out.println("JNDI API lookup failed: " + e.toString());
		}

		/*
		 * Create connection. Create session from connection; false means
		 * session is not transacted. Create sender and text message. Send
		 * messages, varying text slightly. Send end-of-messages message.
		 * Finally, close connection.
		 */
		try {
			queueConnection = queueConnectionFactory.createQueueConnection();
			queueSession = queueConnection.createQueueSession(false,
					Session.AUTO_ACKNOWLEDGE);
			queueSender = queueSession.createSender(queue);
			message = queueSession.createTextMessage();
			/*
			 * message.writeBytes(byteOut.toByteArray());
			 * message.setText("This is message from Preeti guguoguoguoguoguoguog"
			 * );
			 */
			System.out.println("Sending message: ");
			message
					.setText("This is message from Abhinaba guguoguoguoguoguoguog");
			queueSender.send(message, DeliveryMode.PERSISTENT, 4,
					1000 * 60 * 10);
			System.out.println("Message sent");
			/* Send a non-text control message indicating end of messages. */
			// queueSender.send(queueSession.createMessage());
		} catch (JMSException e) {
			System.out.println("Exception occurred: " + e.toString());
		} finally {
			if (queueConnection != null) {
				try {
					queueConnection.close();
				} catch (JMSException e) {
				}
			}
		}
	}

}

package src;
import java.util.Properties;
import javax.jms.*;import javax.naming.*;
public class SimpleQueueReceiver {
public static void main(String[] args) { String queueName = null; Context jndiContext = null; QueueConnectionFactory queueConnectionFactory = null; QueueConnection queueConnection = null; QueueSession queueSession = null; Queue queue = null; QueueSender queueSender = null; TextMessage message = null; QueueReceiver queueReceiver=null; try { Properties p=new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory"); p.put(Context.PROVIDER_URL,"jnp://192.168.192.109:1099"); p.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces"); //Context ctx=new InitialContext(p); jndiContext = new InitialContext(p); } catch (NamingException e) { System.out.println("Could not create JNDI API " + "context: " + e.toString()); System.exit(1); } try { queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("ConnectionFactory"); queue = (Queue) jndiContext.lookup("queue/SSX-SSEIncomingMessageQueue"); } catch (NamingException e) { System.out.println("JNDI API lookup failed: " + e.toString()); System.exit(1); }
/* * Create connection. * Create session from connection; false means session is * not transacted. * Create sender and text message. * Send messages, varying text slightly. * Send end-of-messages message. * Finally, close connection. */ try { queueConnection = queueConnectionFactory.createQueueConnection(); queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queueConnection.start(); //queueSender = queueSession.createSender(queue); queueReceiver = queueSession.createReceiver(queue); message = (TextMessage)queueReceiver.receive(5000); if(message!=null) System.out.println(message.getText()); else System.out.println("No messages for u"); } catch (JMSException e) { System.out.println("Exception occurred: " + e.toString()); } finally { if (queueConnection != null) { try { queueConnection.close(); } catch (JMSException e) {} } } }}

Advertisements

2 Comments Add yours

  1. ale_T_L says:

    I have got some doubt, about jms you can pass one example

  2. ale_T_L says:

    I have got some doubt, about jms you can pass one example .

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