Unable to access ActiveMQ using JMS based code and amqp 1.0

后端 未结 1 844
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-26 02:13

I\'m trying to connect to an ActiveMQ broker using AMQP 1.0, but I want to use JMS within my application code. I\'m interested in using JMS primarily because I want developers t

相关标签:
1条回答
  • 2021-01-26 02:41

    it is better to work with jndi

    public static void main(String[] args) throws JMSException, InterruptedException, NamingException {
        Connection connection = null;
        try {
            Properties props = new Properties();
            props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory");
            props.setProperty("connectionfactory.myFactoryLookup",
                    "amqp://localhost:5672");
            props.put("topic." + "MyTOPIC", "customerTopic");
            InitialContext ic = new InitialContext(props);
            ConnectionFactory cf1 = (ConnectionFactory) ic.lookup("myFactoryLookup");
            Topic topic = (Topic) ic.lookup("MyTOPIC");
            connection = cf1.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(topic);
            connection.start();
            for (int i = 0; i < 10; i++) {
                Message msg = session.createTextMessage("Task : " + i);
                producer.send(msg);
            }
            session.close();
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }
    

    replace

     <dependency>
        <groupId>org.apache.qpid</groupId>
        <artifactId>qpid-amqp-1-0-client-jms</artifactId>
        <version>0.32</version>
    </dependency>
    

    by

        <dependency>
            <groupId>org.apache.qpid</groupId>
            <artifactId>qpid-jms-client</artifactId>
            <version>0.9.0</version>
        </dependency>
    

    on the broker side you need to add:

     <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?transport.transformer=jms"/>
    

    ref http://activemq.apache.org/amqp.html

    0 讨论(0)
提交回复
热议问题