mardi 15 mars 2011

JMS Queue Producer



package com.test;

import java.util.Hashtable;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JmsQueueClient
{
public static void main(String ...strings)
{
try
{
Context getMyContext = null;
try
{
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory" );
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, "jnp://localhost:1199");
env.put(Context.SECURITY_PRINCIPAL, "guest");
env.put(Context.SECURITY_CREDENTIALS, "guest");

getMyContext = new InitialContext(env);

}
catch (NamingException myNamingException)
{
System.out.println("Error establishing Connection" + ": " + myNamingException.toString());
System.exit(1);
}

ConnectionFactory myJMSFactory = null;
Destination myJMSDest = null;
try
{
myJMSFactory = (ConnectionFactory) getMyContext.lookup("ConnectionFactory");
myJMSDest = (Destination) getMyContext.lookup("queue/myAppQueue");
}
catch (Exception e)
{
System.out.println("Lookup failed: " +e);
System.exit(1);
}

Connection myJMSConnection = myJMSFactory.createConnection();

Session myJMSSession = myJMSConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

MessageProducer myJMSProducer = myJMSSession.createProducer(myJMSDest);
TextMessage message = myJMSSession.createTextMessage();
message.setText("Hello there from publisher");

myJMSProducer.send(message);
}
catch (JMSException e)
{
System.out.println(e);
}
}
}

Aucun commentaire: