import javax.jms.*;
import javax.naming.*;
public class ConsumerA implements MessageListener,ExceptionListener { /* 1 */
public static void main( String[] args ) {
...
Connection connection = null;
ConsumerA consumerA = new ConsumerA (); /* 2 */
...
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); /* 3 */
ConnectionFactory connectionFactory = (ConnectionFactory)
initialContext.lookup("java:comp/env/jms/ConnectionFactory"); /* 4 */
Destination destination = (Destination)
initialContext.lookup("java:comp/env/jms/Destination"); /* 5 */
connection = connectionFactory.createConnection(); /* 6 */
connection.setExceptionListener( consumerA ) ; /* 7 */
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); /* 8 */
MessageConsumer consumer = session.createConsumer(destination); /* 9 */
consumer.setMessageListener(consumerA); /* 10 */
connection.start(); /* 11 */
/* 待ち合わせ処理 */
} catch( Exception e ) {
...
}
finally {
if( null != connection ) {
try {
connection.close(); /* 12 */
}
catch( Exception e ) {
...
}
}
}
}
public void onMessage(Message message) {
...
try {
} catch( JMSException e ) {
...
}
...
}
public void onException( JMSException jmsEx ) {
...
}
} |