import javax.jms.*;
import javax.naming.*;
public class SubscriberD {
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 */
Topic topic = (Topic)initialContext.lookup("java:comp/env/jms/Topic"); /* 3 */
connection = connectionFactory.createConnection(); /* 4 */
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); /* 5 */
TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic, "dsub"); /* 6 */
connection.start(); /* 7 */
Message message = topicSubscriber.receive(); /* 8 */
topicSubscriber.close(); /* 9 */
session.unsubscribe("dsub"); /* 10 */
} catch( Exception e ) {
...
}
finally {
if( null != connection ) {
try {
connection.close(); /* 11 */
}
catch( Exception e ) {
...
}
}
}
}
} |