Skip to main content

Testing jms client connections to sun8

Here I had the problem that I did not know whether my jms on sun8 application server was properly configured.

This is what to do to call a queue defined in sun8, send a message and receive a message.
I used an example from sun java tutorial SimpleProducer.java and SimpleSynchConsumer.

import javax.jms.*;
import javax.naming.*;
import java.util.Properties;

public class SimpleProducer {
/**
* Main method.
*
* @param args the destination used by the example
* and, optionally, the number of
* messages to send
*/
public static void main(String[] args) {
final int NUM_MSGS;

if ((args.length <> 2)) {
System.out.println("Program takes one or two arguments: " +
" []");
System.exit(1);
}

String destName = new String(args[0]);
System.out.println("Destination name is " + destName);

if (args.length == 2) {
NUM_MSGS = (new Integer(args[1])).intValue();
} else {
NUM_MSGS = 1;
}

/*
* Create a JNDI API InitialContext object if none exists
* yet.
*/
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.appserv.naming.S1ASCtxFactory");
prop.put(Context.PROVIDER_URL,"iiop://localhost:3700");
Context jndiContext = null;


System.out.println("Created initial context");

/*
* Look up connection factory and destination. If either
* does not exist, exit. If you look up a
* TopicConnectionFactory or a QueueConnectionFactory,
* program behavior is the same.
*/
ConnectionFactory connectionFactory = null;
Destination dest = null;

String connFactName = "XXXConnectionFactory";
try {
System.out.println("Trying to get connection factory");
connectionFactory =
(ConnectionFactory) jndiContext.lookup(connFactName);
} catch (Exception e) {
System.out.println("JNDI API lookup for " + connFactName + " failed: " + e.toString());
e.printStackTrace();
System.exit(1);
}
System.out.println("Retrieved connection factory");

try {
System.out.println("Trying to get destination");
dest = (Destination) jndiContext.lookup(destName);
} catch (Exception e) {
System.out.println("JNDI API lookup for " + destName + " failed: " + e.toString());
e.printStackTrace();
System.exit(1);
}
System.out.println("Retrieved destination");


/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create producer and text message.
* Send messages, varying text slightly.
* Send end-of-messages message.
* Finally, close connection.
*/
Connection connection = null;
MessageProducer producer = null;

try {
connection = connectionFactory.createConnection();

Session session =
connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(dest);

TextMessage message = session.createTextMessage();

for (int i = 0; i < NUM_MSGS; i++) {
message.setText("This is message " + (i + 1));
System.out.println("Sending message: " + message.getText());
producer.send(message);
}

/*
* Send a non-text control message indicating end of
* messages.
*/
//producer.send(session.createMessage());
} catch (JMSException e) {
System.out.println("Exception occurred: " + e.toString());
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
}
}
}
}
}

I changed some small things with respect to the original:
  • I explicitly added some properties before building up the InitialContext; it probably works without as well
  • the factory connection name is here specific.
  • Factory and destination are retrieved separately for ease of debugging.
  • I removed the sending of an empty message.

The program compiles with:
javac -classpath j2ee.jar SimpleProducer.java
To run the program I used the following command line:
java -Dcom.sun.appserv.iiop.endpoints=localhost:3700 -classpath appserv-rt.jar:imqjmsra.jar:j2ee.jar:appserv-admin.jar:. SimpleProducer TestJMSQueue 3

All libraries except the j2ee.jar are specific for the sun8 jms. The jmvarg is also specific for this jms implementation. The program creates 3 messages on TestJmsQueue, and then actually hangs. With ctrl-c I stop it :-).

To read out the messages, I use almost literally:

import javax.jms.*;
import javax.naming.*;


public class SimpleSynchConsumer {
/**
* Main method.
*
* @param args the destination name and type used by the
* example
*/
public static void main(String[] args) {
String destName = null;
Context jndiContext = null;
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination dest = null;
MessageConsumer consumer = null;
TextMessage message = null;

if (args.length != 1) {
System.out.println("Program takes one argument: ");
System.exit(1);
}

destName = new String(args[0]);
System.out.println("Destination name is " + destName);

/*
* Create a JNDI API InitialContext object if none exists
* yet.
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println("Could not create JNDI API context: " +
e.toString());
System.exit(1);
}

/*
* Look up connection factory and destination. If either
* does not exist, exit. If you look up a
* TopicConnectionFactory or a QueueConnectionFactory,
* program behavior is the same.
*/
try {
connectionFactory =
(ConnectionFactory) jndiContext.lookup("XXXConnectionFactory");
dest = (Destination) jndiContext.lookup(destName);
} catch (Exception 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 consumer, then start message delivery.
* Receive all text messages from destination until
* a non-text message is received indicating end of
* message stream.
* Close connection.
*/
try {
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
consumer = session.createConsumer(dest);
connection.start();

while (true) {
Message m = consumer.receive(1);

if (m != null) {
if (m instanceof TextMessage) {
message = (TextMessage) m;
System.out.println("Reading message: " +
message.getText());
} else {
break;
}
}
}
} catch (JMSException e) {
System.out.println("Exception occurred: " + e.toString());
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
}
}
}
}
}


Here I only changed the original connection factory name.
This program compiles/runs in similar way.
javac -classpath j2ee.jar SimpleSynchConsumer.java
java -Dcom.sun.appserv.iiop.endpoints=localhost:3700 -classpath appserv-rt.jar:imqjmsra.jar:j2ee.jar:appserv-admin.jar:. SimpleSynchConsumer TestJMSQueue
The program gets the messages created with the program before.

To test for other environments, make sure you use the implementation specific initial context properties, and have the implementation specific jars in your classpath.

Comments

Popular posts from this blog

Create a groovy console and bind to selenium

Required groovy files In the previous posting we defined the pom file that we need for our build environment. Now we will setup some groovy files to get selenium and groovy running interactively. ConsoleWaiter.groovy The idea of Groovy Console I found on some other sides. Honour goes for instance too: http://josefbetancourt.wordpress.com/tag/eclipse-2/ I copied some code of this, and put it under src/test/groovy/com/jankester/selenium/test/utils: package com.jankester.selenium.test.utils /** * File: ConsoleWaiter.groovy */ import groovy.lang.Binding; import groovy.ui.Console; /** * Provides a wrapper for the console. * * Based on source by John Green * Adapted from: http://www.oehive.org/files/ConsoleWaiter.groovy * Released under the Eclipse Public License * http://www.eclipse.org/legal/epl-v10.html * * I added methods to allow use from Java. * * The run() method launches the console and causes this thread * to sleep until the console's window is closed.

SSL handshake failed: Secure connection truncated

Got this problem on Ubuntu 9.10 and 10.10. svn co --username=xx https:/yy zz “SSL handshake failed: Secure connection truncated” According to this link bug-ubuntu The solution is: sudo apt-get install libneon27 cd /usr/lib/ sudo rm libneon-gnutls.so.27 sudo ln -s /usr/lib/libneon.so.27 libneon-gnutls.so.27

Junit4 running parallel junit classes

To run junit testcases parallel, you can create your own class to run junit with: Add this tag to your class declaration. @RunWith(Parallelized.class) Implementation of this class looks like: package mypackage; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import org.junit.runners.Parameterized; import org.junit.runners.model.RunnerScheduler; public class Parallelized extends Parameterized {         private static class ThreadPoolScheduler implements RunnerScheduler     {         private ExecutorService executor;                 public ThreadPoolScheduler()         {             String threads = System.getProperty("junit.parallel.threads", "16");             int numThreads = Integer.parseInt(threads);             executor = Executors.newFixedThreadPool(numThreads);         }                 public void finished()         {             executor.shutdown();             try