Here is a basic class to connect to a Glassfish 2.1.x JMS Queue from a command line client.
You need the following .jar files in your CLASSPATH for the code below to work.
.../glassfish/imq/lib/imq.jar
.../glassfish/imq/lib/imqutil.jar
.../glassfish/imq/lib/jms.jar
.../glassfish/lib/appserv-admin.jar
.../glassfish/lib/appserv-rt.jar
.../glassfish/lib/j2ee.jar
.../glassfish/lib/javaee.jar
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;
public class Main
{
public static void main(final String[] args)
{
Context jndiContext;
ConnectionFactory connectionFactory;
Connection connection;
Session session;
Queue queue;
MessageProducer messageProducer;
TextMessage message;
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.appserv.naming.S1ASCtxFactory");
properties.put(Context.PROVIDER_URL, "iiop://127.0.0.1:3700"); // had to use ip address, localhost didn't work
try
{
jndiContext = new InitialContext(properties);
connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/ConnectionFactory"); // put your ConnectionFactory here
queue = (Queue) jndiContext.lookup("jms/Queue"); // put your Queue here
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
messageProducer = session.createProducer(queue);
message = session.createTextMessage("Hello World!");
messageProducer.send(message);
connection.close();
}
catch (JMSException e)
{
throw new RuntimeException(e);
}
catch (NamingException e)
{
throw new RuntimeException(e);
}
System.exit(0);
}
}