У меня это работало для 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)
...