ActiveMQ 主题可从 JNDI 发现吗?
我已经为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)
...
I had this working for dynamicQueue/TOPIC1
but my requirement is that I restrict to client connections to known destinations defined in the activemq configuration.
My ActiveMQ configuration is:
<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>
My client-side code is:
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;
}
}
}
But I get
-------------------------------------------------------------------------------
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)
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 JND 成功找到
TopicConnectionFactory
后,请使用以下调用来获取您的主题:Once you have successfully found
TopicConnectionFactory
from JND use following calls to get your topic: