How to use Java JMS with MQseries

后端 未结 6 1465
故里飘歌
故里飘歌 2021-01-30 09:13

I am trying to develop a JMS standalone application to read and write to a Queue on MQSeries. My boss asked me to use pure java JMS (not ibm.m

6条回答
  •  梦谈多话
    2021-01-30 09:58

    If you don't mind writing WMQ-specific code then you can do

    MQConnectionFactory cf = new MQConnectionFactory();
    cf.setHostName(HOSTNAME);
    cf.setPort(PORT);
    cf.setChannel(CHANNEL);
    cf.setQueueManager(QMNAME);
    cf.setTransportType(WMQConstants.WMQ_CM_CLIENT);
    

    then usual JMS resources

    Connection c = cf.createConnection();
    Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue q = s.createQueue("myQueue"); // replace with real queue name
    MessageProducer p = s.createProducer(q);
    

    and finally create and send a message

    Message m = s.createTextMessage("Hello, World!);
    p.send(m);
    

    (i've typed that in off the top of my head so can't rule out a typo, but it's fundamentally correct). If you're really supposed to be using 'pure JMS' - i.e. with no provider-specific objects - then you need to bind a MQConnectionFactory object in JNDI (take a look at the JMSAdmin tool, it's in the docs) then look it up from your application, i.e.

    InitialContext ic = new InitialContext(); // or as appropraite
    ConnectionFactory cf = (ConnectionFactory)ic.lookup("myMQfactory"); // replace with JNDI name
    

提交回复
热议问题