我的这个适用于dynamicQueue/TOPIC1
但我的要求是我限制为客户端连接到activemq配置中定义的已知目标。
我的ActiveMQ配置是:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd">
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.base}/data">
<destinations>
<topic physicalName="TOPIC1"/>
</destinations>
<plugins>
<simpleAuthenticationPlugin anonymousAccessAllowed="true"/>
<authorizationPlugin>
<map>
<authorizationMap>
<authorizationEntries>
<authorizationEntry topic=">" read="anonymous" write="anonymous" admin="anonymous"/>
<authorizationEntry topic="ActiveMQ.Advisory.>" read="anonymous" write="anonymous" admin="anonymous"/>
</authorizationEntries>
</authorizationMap>
</map>
</authorizationPlugin>
</plugins>
<transportConnectors>
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?trace=true"/>
</transportConnectors>
</broker>
</beans>
我的客户端代码是:
public class JndiLookupIT {
private Context ctx;
@Before
public void init() throws NamingException {
final Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616");
ctx = new InitialContext(props);
}
@Test
public void testLookupTopic() throws Exception {
final TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("ConnectionFactory");
final Topic mytopic = (Topic) ctx.lookup("TOPIC1");
final JmsTemplate jmsTemplate = new JmsTemplate(factory);
final MessageCreator mc = new TextMessageCreator();
jmsTemplate.send(mytopic, mc);
}
private static class TextMessageCreator implements MessageCreator {
@Override
public Message createMessage(final Session session) throws JMSException {
final ActiveMQTextMessage activeMQTextMessage = new ActiveMQTextMessage();
activeMQTextMessage.setText("Hello");
return activeMQTextMessage;
}
}
}
但是我得到了
-------------------------------------------------------------------------------
Test set: com.mycompany.JndiLookupIT
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.438 sec <<< FAILURE!
testLookupTopic(com.mycompany.JndiLookupIT) Time elapsed: 0.375 sec <<< ERROR!
javax.naming.NameNotFoundException: TOPIC1
at org.apache.activemq.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:235)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.mycompany.JndiLookupIT.testLookupTopic(JndiLookupIT.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
答案 0 :(得分:0)
从JND成功找到TopicConnectionFactory
后,请使用以下调用来获取您的主题:
boolean transacted = false;
TopicConnection connection = factory.createConnection();
connection.start();
TopicSession session =
connection.createTopicSession(transacted, Session.AUTO_ACKNOWLEDGE);
Topic myTopic = this.session.createTopic("TOPIC1");