谁有IBM MQ 和 java jms 的例子啊
答案:1 悬赏:80 手机版
解决时间 2021-11-18 09:25
- 提问者网友:爱了却不能说
- 2021-11-17 12:12
谁有IBM MQ 和 java jms 的例子啊
最佳答案
- 五星知识达人网友:归鹤鸣
- 2021-11-17 13:10
别人弄的,我现在还在调,还没有通过。下面的代码应该没有问题
package com.ibm.mq.test;
import java.util.Hashtable;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.ibm.mq.jms.MQQueueConnectionFactory;
public class MQSendByCF_JNDI {
public static void main(String[] args) {
MQSendByCF_JNDI sender = new MQSendByCF_JNDI();
try {
sender.initMQObjects();
sender.sendMsg();
} catch(Exception e) {
e.printStackTrace();
} finally {
sender.closeMQObjects();
}
}
MQQueueConnectionFactory mqcf;
QueueConnection conn;
QueueSession session;
TextMessage textMsg;
Queue queue;
QueueSender sender;
public void initMQObjects() throws Exception {
String strMsg = "Where are you?";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:/D:/JNDI");
try {
Context ctx = new InitialContext(env);
mqcf = (MQQueueConnectionFactory) ctx.lookup("QCF_TEST");
queue = (Queue) ctx.lookup("Q_TEST");
} catch (NamingException e) {
System.out.println("Find MQ Objects from Context Failed.");
throw e;
}
conn = mqcf.createQueueConnection();
session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
textMsg = session.createTextMessage(strMsg);
sender = session.createSender(queue);
}
public void sendMsg() throws Exception {
try {
conn.start();
} catch (JMSException e) {
System.out.println("Send Msg Failed.");
throw e;
}
sender.send(textMsg,DeliveryMode.NON_PERSISTENT,0,0);
System.out.println("Send Msg succeed.");
}
public void closeMQObjects() {
try {
if(conn != null) {
conn.stop();
}
if(sender != null) {
sender.close();
}
if(session != null) {
session.close();
}
if(conn != null) {
conn.close();
}
if(mqcf != null) {
mqcf.clear();
}
System.out.println("Close MQ objects succeed.");
} catch (JMSException e) {
System.out.println("Close MQ objects failed: " + e.getMessage());
}
}
}
package com.ibm.mq.test;
import java.util.Hashtable;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.ibm.mq.jms.MQQueueConnectionFactory;
public class MQSendByCF_JNDI {
public static void main(String[] args) {
MQSendByCF_JNDI sender = new MQSendByCF_JNDI();
try {
sender.initMQObjects();
sender.sendMsg();
} catch(Exception e) {
e.printStackTrace();
} finally {
sender.closeMQObjects();
}
}
MQQueueConnectionFactory mqcf;
QueueConnection conn;
QueueSession session;
TextMessage textMsg;
Queue queue;
QueueSender sender;
public void initMQObjects() throws Exception {
String strMsg = "Where are you?";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:/D:/JNDI");
try {
Context ctx = new InitialContext(env);
mqcf = (MQQueueConnectionFactory) ctx.lookup("QCF_TEST");
queue = (Queue) ctx.lookup("Q_TEST");
} catch (NamingException e) {
System.out.println("Find MQ Objects from Context Failed.");
throw e;
}
conn = mqcf.createQueueConnection();
session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
textMsg = session.createTextMessage(strMsg);
sender = session.createSender(queue);
}
public void sendMsg() throws Exception {
try {
conn.start();
} catch (JMSException e) {
System.out.println("Send Msg Failed.");
throw e;
}
sender.send(textMsg,DeliveryMode.NON_PERSISTENT,0,0);
System.out.println("Send Msg succeed.");
}
public void closeMQObjects() {
try {
if(conn != null) {
conn.stop();
}
if(sender != null) {
sender.close();
}
if(session != null) {
session.close();
}
if(conn != null) {
conn.close();
}
if(mqcf != null) {
mqcf.clear();
}
System.out.println("Close MQ objects succeed.");
} catch (JMSException e) {
System.out.println("Close MQ objects failed: " + e.getMessage());
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯