简体   繁体   English

如何从WAS上的Java应用程序中将WAS JMS资源用于IIB MQ队列(使用JNDI)

[英]How to use WAS JMS resourses for IIB MQ Queue from a Java Application on WAS (using JNDI)

I am new to MQ and JNDI and I looking for some simple sample Java code that resolves my WAS JMS configuration and can write to and read from two Message Queues. 我是MQ和JNDI的新手,我正在寻找一些简单的Java示例代码来解析我的WAS JMS配置,并且可以读写两个Message Queue。

specifically I would like JAVA code to: 具体来说,我希望JAVA代码能够:

  • run code on IBM WebSphere Application Server Network Deployment (WAS ND 8.5.5) 在IBM WebSphere Application Server网络部署(WAS ND 8.5.5)上运行代码
  • write to, and read from, 2 IBM Integration Bus (IIB) Message Queues on an external system 在外部系统上写入和读取2个IBM Integration Bus(IIB)消息队列

  • In WAS I configured JMS resources as follows: 在WAS中,我按如下方式配置了JMS资源:

    • for the connection factory - gave it a JNDI name of "jms/MQCONN.FACTORY" 连接工厂-给它一个JNDI名称“ jms / MQCONN.FACTORY”
    • for the queue #1 - gave it a JNDI name of "jms/MQUEUE1.DEST" 对于队列#1-为其指定了JNDI名称“ jms / MQUEUE1.DEST”
    • for the queue #2 - gave is a JNDI name of "jms/MQUEUE2.DEST" 对于队列#2-给定的是JNDI名称“ jms / MQUEUE2.DEST”

I set up JAAS - J2C authentication data credentials. 我设置了JAAS-J2C认证数据凭证。

Note: I was unable to test the connection to MQ for connection factory, because the security settings are added to after the wizard completes and the you can only test from the wizard. 注意:我无法为连接工厂测试与MQ的连接,因为安全设置是在向导完成后添加到的,并且您只能从向导进行测试。 I believe the WAS configuration is correct including the credentials. 我相信包括凭据在内的WAS配置是正确的。

I especially do not understand how to code the JNDI part (ie How to store the environment variable that tells JNDI which initial context to use, and where to find the provider.) 我特别不理解如何编码JNDI部分(即,如何存储环境变量,该变量告诉JNDI使用哪个初始上下文以及在哪里找到提供者。)

Grateful for any assistance! 感谢您的协助!

Sibyl , Once you have configured these Managed Objects (QueueConnectionFactory . Queue) , you should be able to lookup these from code that you can deploy on the application server. Sibyl,配置了这些托管对象(QueueConnectionFactory。Queue)之后,您应该能够从可以在应用程序服务器上部署的代码中查找这些对象。

You will have to get 你将不得不

a) InitialContext (when you deploy a ear on the server , you can use the default constructor) a)InitialContext(在服务器上部署耳朵时,可以使用默认构造函数)

b) Lookup queue connection factory (context.lookup(xxx)) b)查找队列连接工厂(context.lookup(xxx))

c) Lookup queue (context.lookup(yyyy)) c)查找队列(context.lookup(yyyy))

d) Create a message producer d)创建一个消息产生者

e) Create a Queue Session , text message and send the message directly e)创建队列会话,文本消息并直接发送消息

You can get some more idea here ( http://www.webspheretools.com/sites/webspheretools.nsf/docs/Creating%20a%20Queue%20Connection%20Factory%20and%20Queue%20for%20connectivity%20to%20MQ ) 您可以在此处获得更多想法( http://www.webspheretools.com/sites/webspheretools.nsf/docs/Creating%20a%20Queue%20Connection%20Factory%20and%20Queue%20for%20connectivity%20to%20MQ

Basically post configuration it is a lot of Boilerplate JMS coding 基本上后配置是很多样板JMS编码

Here is little help for you. 这对您几乎没有帮助。 You don't need to provide extra configuration after creating resources in WAS. 在WAS中创建资源后,您无需提供额外的配置。

Queue myQueue;
QueueConnectionFactory myQueueFactory;
QueueConnection connection = null;
QueueSession session = null;

try{
    InitialContext jndi = new InitialContext();
    myQueueFactory = (QueueConnectionFactory) jndi.lookup("jms/MQCONN.FACTORY");
    myQueue = (Queue) jndi.lookup("jms/MQUEUE1.DEST");

    connection=myQueueFactory.createQueueConnection();

    session = connection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);

    QueueSender sender = session.createSender(myQueue);
    connection.start();
    TextMessage textMessage = session.createTextMessage(event);
    textMessage.setStringProperty("messageType", "file");
    sender.send(textMessage);

    sender.close();

    if (session != null) {
        session.close();
    }

    if (connection != null) {
        connection.close();
    }
} catch (JMSException e) {
    e.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM