Try this after setting up the environment for JMS. The programs are purely for Jboss environment.
SimpleQueueSender
package src;
import javax.naming.InitialContext;
import javax.jms.ConnectionFactory;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import javax.jms.Queue;
import java.util.Properties;
import javax.jms.*;
import javax.naming.*;
import java.io.*;
public class SimpleQueueSender{
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; // Message message = null; FileUpload fload=null; File f=null; /*f=new File("d:/ex.zip"); fload=new FileUpload(); fload.setFile(f); ByteArrayOutputStream byteOut=fload.getByteStream();*/ 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()); 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); 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) {} } } }}
Queue Receiver
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) {} } } }}
0 comments:
Post a Comment