How to connect to RabbitMQ using RabbitMQ JMS client from an existing JMS application?

后端 未结 3 1949
我在风中等你
我在风中等你 2021-02-06 10:57

I have a generic standalone JMS application which works with following JMS providers WebSphere, HornetQ and ActiveMq. I pass Context.INITIAL_CONTEXT_FACTORY and Context.PROVIDER

3条回答
  •  感动是毒
    2021-02-06 11:18

    In order to get JMS working with RabbitMQ, you have to enable the plugin rabbitmq_jms_topic_exchange.
    You can download it following the directions in this site (You'll need to login):
    https://my.vmware.com/web/vmware/details?downloadGroup=VFRMQ_JMS_105&productId=349

    1. After extraction, put the file rjms-topic-selector-1.0.5.ez inside the Folder $RABBITMQ_SERVER\plugins.
    2. Enable the plugin with the command: rabbitmq-plugins enable rabbitmq_jms_topic_exchange
    3. Check if the plugin it's running ok with the command: rabbitmq-plugins list
    4. Restart the RabbitMQ - I'm not sure if it's really necessary, but just in case ;-)
    5. At your RabbitMQ web management (http://localhost:15672/#/exchanges) you can check the new Exchange you have available:
    6. Now, in theory :-), you're already able to connect to your RabbiMQ server using the standard Java JMS API.

    Bear in mind that you'll have to create a .bindings file in order to JNDI found your registered objects. This is an example of the content of it:

    
        ConnectionFactory/ClassName=com.rabbitmq.jms.admin.RMQConnectionFactory
        ConnectionFactory/FactoryName=com.rabbitmq.jms.admin.RMQObjectFactory
        ConnectionFactory/RefAddr/0/Content=jms/ConnectionFactory
        ConnectionFactory/RefAddr/0/Type=name
        ConnectionFactory/RefAddr/0/Encoding=String
        ConnectionFactory/RefAddr/1/Content=javax.jms.ConnectionFactory
        ConnectionFactory/RefAddr/1/Type=type
        ConnectionFactory/RefAddr/1/Encoding=String
        ConnectionFactory/RefAddr/2/Content=com.rabbitmq.jms.admin.RMQObjectFactory
        ConnectionFactory/RefAddr/2/Type=factory
        ConnectionFactory/RefAddr/2/Encoding=String
        # Change this line accordingly if the broker is not at localhost
        ConnectionFactory/RefAddr/3/Content=localhost
        ConnectionFactory/RefAddr/3/Type=host
        ConnectionFactory/RefAddr/3/Encoding=String
        # HELLO Queue 
        HELLO/ClassName=com.rabbitmq.jms.admin.RMQDestination
        HELLO/FactoryName=com.rabbitmq.jms.admin.RMQObjectFactory
        HELLO/RefAddr/0/Content=jms/Queue
        HELLO/RefAddr/0/Type=name
        HELLO/RefAddr/0/Encoding=String
        HELLO/RefAddr/1/Content=javax.jms.Queue
        HELLO/RefAddr/1/Type=type
        HELLO/RefAddr/1/Encoding=String
        HELLO/RefAddr/2/Content=com.rabbitmq.jms.admin.RMQObjectFactory
        HELLO/RefAddr/2/Type=factory
        HELLO/RefAddr/2/Encoding=String
        HELLO/RefAddr/3/Content=HELLO
        HELLO/RefAddr/3/Type=destinationName
        HELLO/RefAddr/3/Encoding=String
    
    

    And then... finally... the code:

    
        Properties environmentParameters = new Properties();
        environmentParameters.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
        environmentParameters.put(Context.PROVIDER_URL, "file:/C:/rabbitmq-bindings");
        namingContext = new InitialContext(environmentParameters);
    
        ConnectionFactory connFactory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
    
    

提交回复
热议问题