[ローカルトランザクションのMessageConsumer]
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 */ connection = connectionFactory.createConnection(); /* 4 */ Session session = connection.createSession(true, 0); /* 5 */ MessageConsumer consumer = session.createConsumer(destination); /* 6 */ connection.start(); /* 7 */ Message message = consumer.receive(); /* 8 */ session.commit(); /* 9 */ } catch( Exception e ) { ... } finally { if( null != connection ) { try { connection.close(); /* 10 */ } catch( Exception e ) { ... } } } } }
JNDIの開始コンテキストを構築します。
ConnectionFactoryオブジェクトを取得します(JNDI名が“ConnectionFactory”の場合)。
Destinationオブジェクトを取得します(JNDI名が“Destination”の場合)。
Connectionを作成します。
Sessionを作成します。
MessageConsumerを作成します。
接続によるメッセージの配信を開始します。
メッセージを受信します。
コミットします。
Connectionをクローズします。
注意
JNDIの開始コンテキスト構築時の環境プロパティの指定については、“J2EEアプリケーションクライアント”を参照してください。