import javax.jms.*;
import javax.naming.*;
public class Consumer {
public static void main( String[] args ) {
Connection connection = null;
...
try {
java.util.Hashtable env = new java.util.Hashtable( );
env.put ( javax.naming.Context.INITIAL_CONTEXT_FACTORY ,
"com.fujitsu.interstage.j2ee.jndi.InitialContextFactoryForClient" );
InitialContext initialContext = new InitialContext(env); /* 1 */
ConnectionFactory connectionFactory = (ConnectionFactory)
initialContext.lookup("java:comp/env/jms/ConnectionFactory"); /* 2 */
Destination destination = (Destination)
initialContext.lookup("java:comp/env/jms/Destination"); /* 3 */
javax.transaction.UserTransaction ut =(javax.transaction.UserTransaction)
initialContext.lookup("java:comp/UserTransaction"); /* 4 */
connection = connectionFactory.createConnection(); /* 5 */
Session session = connection.createSession(true, 0); /* 6 */
MessageConsumer consumer = session.createConsumer(destination); /* 7 */
connection.start(); /* 8 */
ut.begin(); /* 9 */
Message message = consumer.receive(); /* 10 */
ut.commit(); /* 11 */
} catch( Exception e ) {
...
}
finally {
if( null != connection ) {
try {
connection.close(); /* 12 */
}
catch( Exception e ) {
...
}
}
}
}
} |