0

我们正在将应用程序从 Jboss 5 迁移到 jboss 7。在此我们需要更改 JMS 的 JNDI 查找。

在 Jboss 5 中,它是由 -

     InitialContext ic = new InitialContext();
    Object obj = ic.lookup(listenerName);
    // listenerName has our destination queue name

在 Jboss 7 中,我们正在尝试 -

        Properties jndiProps = new Properties();
        jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        jndiProps.put(Context.PROVIDER_URL,"http-remoting://localhost:8080");
        jndiProps.put("jboss.naming.client.ejb.context", new Boolean(true));
        InitialContext ic = new InitialContext(jndiProps);
        Object obj = ic.lookup("jms/queue/" + listenerName);

在 Standalone-full.xml 我们添加了目标队列 -

<jms-queue name="CORESERVICES.DEMO_QUEUE" entries="java:/jms/queue/CORESERVICES.DEMO_QUEUE"/>

我们收到这些错误 -

2017-03-31 15:44:04,056 INFO  [com.praval.services.core.msg.MessageRouterMDB] (Thread-16 (ActiveMQ-client-global-threads-1938238098)) PRAVAL jmsQueueName is CORESERVICES.DEMO_QUEUE
2017-03-31 15:44:04,195 INFO  [org.jboss.ejb.client.remoting] (Remoting "config-based-naming-client-endpoint" task-6) EJBCLIENT000017: Received server version 2 and marshalling strategies [river]
2017-03-31 15:44:04,203 INFO  [org.jboss.ejb.client.remoting] (Thread-16 (ActiveMQ-client-global-threads-1938238098)) EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@13279398, receiver=Remoting connection EJB receiver [connection=Remoting connection <3a06c684>,channel=jboss.ejb,nodename=pravalsharma]} on channel Channel ID 45654a55 (outbound) of Remoting connection 12baae23 to localhost/127.0.0.1:8080
2017-03-31 15:44:04,302 WARNING [com.praval.services.core.msg.MessageRouterMDB] (Thread-16 (ActiveMQ-client-global-threads-1938238098)) javax.naming.NameNotFoundException: jms/queue/CORESERVICES.DEMO_QUEUE -- service jboss.naming.context.java.jboss.exported.jms.queue."CORESERVICES.DEMO_QUEUE"
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:184)
at org.jboss.naming.remote.protocol.v1.Protocol$1.handleServerMessage(Protocol.java:127)
at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever$1.run(RemoteNamingServerV1.java:73)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
4

1 回答 1

0

如果 JMS 在运行代码的 jboss 上,那么您可以在 CDI 中使用以下代码

@Singleton
@Transactional(value = TxType.REQUIRES_NEW)
public class ServiceLayer implements IServiceLayer {
    @Resource(mappedName = "ConnectionFactory")
    public ConnectionFactory localQueueFactory;

    @Resource(mappedName = "jms/queue/CORESERVICES.DEMO_QUEUE")
    public Queue queue;

    public void sendMessage2(String msge) throws Exception {
        MessageProducer qsender = null;
        Session qsession = null;
        Connection qcon = null;
        try {
            qcon = this.localQueueFactory.createConnection();
            qsession = qcon.createSession(true, QueueSession.AUTO_ACKNOWLEDGE);
            qsender = qsession.createProducer(queue);
            int i = 1;
            for (int j = 0; j < i; j++) {
                TextMessage tm = qsession.createTextMessage(msge);
                qsender.send(tm);
                System.out.println("Message [" + tm.getText() + "] sent to Queue: " + queue.getQueueName());
            }

        } catch (Exception e) {
            System.out.println("Exception : " + e.getMessage());
            e.printStackTrace();
        } finally {
            qcon.close();
        }
    }
}

如果您不使用 CDI 或 EJB,那么您不必为上下文提供环境变量,您只需执行以下操作

public final static String QUEUE = "jms/queue/CORESERVICES.DEMO_QUEUE";
public void sendMessage() throws Exception {


            // Define queue
            QueueSender qsender = null;
            QueueSession qsession = null;
            QueueConnection qcon = null;
            try {
                InitialContext ctx = new InitialContext();//It will take the initial context of the container on which the code is deployed. 

                QueueConnectionFactory qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
                qcon = qconFactory.createQueueConnection();

                qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
                Queue queue = (Queue) ctx.lookup(QUEUE);

                TextMessage msg = qsession.createTextMessage();
                msg.setText("<xml>textMessage" + "</xml>");

                qsender = qsession.createSender(queue);
                qsender.send(msg);

                System.out.println("Message [" + msg.getText() + "] sent to Queue: " + QUEUE);
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (qsender != null)
                    qsender.close();
                if (qsession != null)
                    qsession.close();
                if (qcon != null)
                    qcon.close();
            }
        }
于 2017-03-31T13:58:22.403 回答