は、ここでは一例
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL,
"tcp://localhost:61616");
props.put("topic." + "TOPICNAME", "TOPICNAME");
InitialContext ic = new InitialContext(props);
ConnectionFactory cf1 = (ConnectionFactory) ic.lookup("ConnectionFactory");
writeDestination = (Topic) ic.lookup("TOPICNAME");
writeDestConnection = cf1.createConnection("user", "pwd");
writeDestConnection.setClientID("durableSubscriber_" + "TOPICNAME");
writeDestSession = writeDestConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
writeDestProducer = writeDestSession.createProducer(writeDestination);
writeDestConnection.start();
TextMessage message = writeDestSession.createTextMessage(json);
message.setStringProperty("clientID", "ifYouNeed");
writeDestProducer.send(message);
http://activemq.apache.org/jndi-support.html
あるJNDIのない別の例である:あなたの答えのための
public static void main(String[] args) throws JMSException {
Connection conn = null;
try {
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
conn = cf.createConnection("user", "pwd");
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(session.createQueue("queueName"));
conn.start();
TextMessage msg = null;
while ((msg = (TextMessage) consumer.receive()) != null) {
System.out.println("Received message is: " + msg.getText());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
}
}
}
}
感謝Hassenを! JNDIを使用せずにActiveMQのキューに接続する別の方法がある場合、私は思ったんだけど? – tkyass
確かに、私は申し訳ありません、消費者が、この部分(プロデューサーコード)と私の応答を更新 –
私の応答更新 –