Unable to access ActiveMQ using JMS based code and amqp 1.0

后端 未结 1 843
佛祖请我去吃肉
佛祖请我去吃肉 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

     
        org.apache.qpid
        qpid-amqp-1-0-client-jms
        0.32
    
    

    by

        
            org.apache.qpid
            qpid-jms-client
            0.9.0
        
    

    on the broker side you need to add:

     
    

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

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