質問
JNDIを使用してActiveMQに接続するだけです。
私は
を持っています
-
「example.A」という名前のキュー。
-
ConectionFactoriesとJNDI経由のキュー(トピック)、クラスパスにjndi.propertiesファイルを配置する必要があります。私が理解したように、activeMQクラスパスはデフォルトで%activemq%/ confディレクトリです。変更していません。 したがって、キューにこのプロパティがあります:
queue.MyQueue = example.A
-
以下のようにJNDIを使用するActiveMQ用のJavaクライアントクラスを作成しました。
Properties jndiParameters = new Properties() ; jndiParameters.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory"); jndiParameters.put(Context.PROVIDER_URL, "tcp://localhost:61616"); Context context = new InitialContext(jndiParameters); ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory"); Queue queue = (Queue) context.lookup("MyQueue");
しかし、キューを見つけることができず、例外をスローします:javax.naming.NameNotFoundException:MyQueue
私の間違いはどこですか
解決
問題は、プロパティを明示的に作成し、それらをInitialContextコンストラクターに渡すことです。これは、クラスパス上のjndi.propertiesが読み取られないことを意味します。
コードは次のようになります。
Context context = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory");
Queue queue = (Queue) context.lookup("MyQueue");
他のヒント
静的プロパティを設定するだけでなく、ファイルから取得することもできます:
InputStream is = getClass().getResourceAsStream("/my.jndi.properties");
Properties jndiParameters = new Properties();
jndiParameters.load(is);
jndiParameters.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
jndiParameters.put(Context.PROVIDER_URL, "tcp://localhost:61616");
Context ctx = new InitialContext(jndiParameters);
...
これは、リソースをロードした後に静的プロップを設定する限り機能します。たとえば、プロバイダーのURLを他の場所から読み込む場合に役立ちます。
所属していません StackOverflow