ActiveMQ 主题可从 JNDI 发现吗?

发布于 2024-11-02 06:13:52 字数 3722 浏览 2 评论 0原文

我已经为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随梦而飞# 2024-11-09 06:13:52

从 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");

Once you have successfully found TopicConnectionFactory from JND use following calls to get your topic:

boolean transacted = false;
TopicConnection connection = factory.createConnection();
connection.start();
TopicSession session = 
                connection.createTopicSession(transacted, Session.AUTO_ACKNOWLEDGE);
Topic myTopic = this.session.createTopic("TOPIC1");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文