Jboss eap 7.0 - 如何在jboss 7中对jms进行JNDI查找

时间:2017-03-31 10:21:39

标签: java jboss jms jboss-eap-7

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

在Jboss 5中,它由 -

完成
{"m":true,
"s":true,
"q":{
"eur":{"b":"1.06783","a":"1.06790","h":"1.06978","l":"1.06729"},
"gbp":{"b":"1.24407","a":"1.24416","h":"1.25093","l":"1.24397"},
"aud":{"b":"0.76456","a":"0.76464","h":"0.76630","l":"0.76387"},
"cad":{"b":"1.33550","a":"1.33557","h":"1.33583","l":"1.33251"},
"jpy":{"b":"111.996","a":"112.002","h":"112.204","l":"111.700"},
"chf":{"b":"1.00110","a":"1.00117","h":"1.00188","l":"0.99850"},
"xau":{"b":"1241.75","a":"1242.1","h":"1244.69","l":"1239.53"},
"xag":{"b":"18.056","a":"18.098","h":"18.175","l":"18.02"},
"nok":{"b":"8.58853","a":"8.5901","h":"8.59342","l":"8.55128"},
"sek":{"b":"8.9308","a":"8.9323","h":"8.9515","l":"8.92151"},
"sgd":{"b":"1.3975","a":"1.3977","h":"1.3959","l":"1.3987"},
"pkr":{"b":"104.8300","a":"105.8300","h":"104.8300","l":"104.620"},
"dkk":{"b":"6.9645","a":"6.9648","h":"6.9500","l":"6.9691"},
"nzd":{"b":"0.69835","a":"0.69859","h":"0.70084","l":"0.69794"},
"zar":{"b":"13.3688","a":"13.37396","h":"13.6475","l":"13.232"},
"hkd":{"b":"7.77017","a":"7.77098","h":"7.77244","l":"7.76847"},
"inr":{"b":"64.8500","a":"64.8600","h":"64.925","l":"64.7800"},
"try":{"b":"3.63447","a":"3.63558","h":"3.65941","l":"3.63076"},
"pln":{"b":"3.9514","a":"3.9520","h":"3.9361","l":"3.9520"},
"cnh":{"b":"6.88261","a":"6.88352","h":"6.89029","l":"6.8706"},
"myr":{"b":"0.2255","a":"0.2265","h":"0.2253","l":"0.2257"},
"sar":{"b":"0.2662","a":"0.2672","h":"0.2662","l":"0.2662"}
}}

在Jboss 7中我们正在尝试 -

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

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

        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);

我们收到了这些错误 -

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

1 个答案:

答案 0 :(得分: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();
            }
        }
相关问题