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

后端 未结 3 1884
我在风中等你
我在风中等你 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:26

    We can generate the .bindings file for RabbitMQ using below java code:

    import java.util.Properties;
    import javax.jms.ConnectionFactory;
    import javax.jms.Queue;
    import javax.jms.Topic;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.Reference;
    import javax.naming.StringRefAddr;
    
    Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
        env.put(Context.PROVIDER_URL, "file:bindings");
        Context ctx = new InitialContext(env);
    
        Reference connectionFactoryRef = new Reference(ConnectionFactory.class.getName(), RMQObjectFactory.class.getName(), null);
        connectionFactoryRef.add(new StringRefAddr("name", "jms/ConnectionFactory"));
        connectionFactoryRef.add(new StringRefAddr("type", ConnectionFactory.class.getName()));
        connectionFactoryRef.add(new StringRefAddr("factory", RMQObjectFactory.class.getName()));
        connectionFactoryRef.add(new StringRefAddr("host", "$JMS_RABBITMQ_HOST$"));
        connectionFactoryRef.add(new StringRefAddr("port", "$JMS_RABBITMQ_PORT$"));
        ctx.rebind("ConnectionFactory", connectionFactoryRef);
    
        String jndiAppend = "jndi";
        for (int i = 1; i <= 10; i++) {
            String name = String.format("queue%02d", i);
            Reference ref = new Reference(Queue.class.getName(), com.rabbitmq.jms.admin.RMQObjectFactory.class.getName(), null);
            ref.add(new StringRefAddr("name", "jms/Queue"));
            ref.add(new StringRefAddr("type", Queue.class.getName()));
            ref.add(new StringRefAddr("factory", RMQObjectFactory.class.getName()));
            ref.add(new StringRefAddr("destinationName", name));
            ctx.rebind(name+jndiAppend, ref);
    
            name = String.format("topic%02d", i);
            ref = new Reference(Topic.class.getName(), com.rabbitmq.jms.admin.RMQObjectFactory.class.getName(), null);
            ref.add(new StringRefAddr("name", "jms/Topic"));
            ref.add(new StringRefAddr("type", Topic.class.getName()));
            ref.add(new StringRefAddr("factory", RMQObjectFactory.class.getName()));
            ref.add(new StringRefAddr("destinationName", name));
            ctx.rebind(name+jndiAppend, ref);
        }
    

提交回复
热议问题