JAVA多线程监听JMS、MQ队列

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/woshiyuhao/article/details/84708436
背景:消息队列中有非常多的消息需要处理,并且监听器onMessage()方法中的业务逻辑也相对比较复杂,为了加快队列消息的读取、处理速度。可以通过加快读取速度和加快处理速度来考虑。因此从这两个方面都使用多线程来处理。对于消息处理的业务处理逻辑用线程池来做。对于加快消息监听读取速度可以使用1.使用多个监听器监听一个队列;2.使用一个监听器开启多线程监听。
对于上面提到的方法2使用一个监听器开启多线程监听,借助spring自带的DefaultMessageListenerContainer可以很方便实现这一功能。
	<!-- 使用spring进行配置 监听-->
<bean id="testListenerContainer1"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="destination" ref="queue2"></property>
<property name="messageListener" ref="messageReceiver"></property>
<property name="sessionTransacted" value="false"></property>
<!-- 设置固定的线程数 -->
<property name="concurrentConsumers" value="6"></property>
<!-- 设置动态的线程数 -->
<property name="concurrency" value="2-9"></property>
<!-- 设置最大的线程数 -->
<property name="maxConcurrentConsumers" value="15"></property>
</bean>

监听器打印输出当前线程:
 public void onMessage(Message message) {
System.out.println("在onMessage中线程ID是"+Thread.currentThread());
展开阅读全文

IBM MQ/JMS 错误

07-02

最近在做IBM MQ的一个东西,在本地队列管理器建一个本地队列,想往里面写入/读出消息rnrnrnpackage com.fantasy.mq;rnrnimport java.io.IOException;rnimport java.util.Hashtable;rnrnimport com.ibm.mq.MQException;rnimport com.ibm.mq.MQMessage;rnimport com.ibm.mq.MQPutMessageOptions;rnimport com.ibm.mq.MQQueue;rnimport com.ibm.mq.MQQueueManager;rnrnpublic class MQSamplern //定义队列管理器和队列的名称 rn private static String qmName = "QM_fantasy"; rn private static String qName = "q1";rn rn private static MQQueueManager qMgr; rn private static Hashtable properties = new Hashtable();rnrn public static void main(String[] args) rn try rn properties.put("hostname", "localhost"); //172.18.136.216rn properties.put("port", new Integer(1414));rn properties.put("channel", "DC.SVRCONN");rn properties.put("CCSID", new Integer(1381));rn properties.put("transport","MQSeries");rn rn // Create a connection to the queue manager rn qMgr = new MQQueueManager(qmName,properties); rn // Set up the options on the queue we wish to open... rn int openOptions = 16;rn // Now specify the queue that we wish to open, rn // and the open options... rn MQQueue remoteQ = qMgr.accessQueue(qName, openOptions); rn rn // Define a simple WebSphere MQ message, and write some text in UTF format.. rn MQMessage putMessage = new MQMessage(); rn putMessage.writeUTF("Test"); rn // specify the message options... rn MQPutMessageOptions pmo = new MQPutMessageOptions(); rn // accept the defaults, same as MQPMO_DEFAULTrn // put the message on the queue rn remoteQ.put(putMessage, pmo); rn System.out.println("Message has been input into the Remote Queue");rnrn // Close the queue... rn remoteQ.close(); rn // Disconnect from the queue manager rn qMgr.disconnect(); rn catch (MQException ex) rn // If an error has occurred in the above, try to identify what went wrong rn // Was it a WebSphere MQ error? rn System.out.println("A WebSphere MQ error occurred : Completion code " + ex.completionCode + rn " Reason code " + ex.reasonCode); rn catch (IOException ex) rn // Was it a Java buffer space error? rn System.out.println("An error occurred whilst writing to the message buffer: " + ex); rn catch(Exception ex)rn ex.printStackTrace();rn rn rnrnrnrnrnMQJE001:发生 MQException:完成码 2,原因 2009rnMQJE016:连接期间,MQ 队列管理器立即关闭通道,rn\u5173闭原因 = 2009rnMQJE001:完成码 2,原因 2009rnA WebSphere MQ error occurred : Completion code 2 Reason code 2009rnrnrnrn不知道为什么 求助啊 谢谢拉 论坛

IBM MQjms的问题

05-12

最近公司要求学习IBM MQ 和jms。现在头都大了。rn 在网上找了些例子。 运行报Exception in thread "main" java.lang.UnsatisfiedLinkError: no mqjbnd05 in java.library.pathrn [code=Java]rn public class Testerrnrnrnrn public static String icf = "com.sun.jndi.fscontext.RefFSContextFactory";rn rn public static String url = "file:/C:/JNDI-Directory";rn rn public static void main(String[] args) throws JMSException, NamingException rn rn rn QueueSession session = null;rn rn QueueConnection connection = null;rn rn QueueConnectionFactory factory = null;rn rn QueueSender queueSender = null;rn rn QueueReceiver queueReceiver= null;rn rn Queue oQueue = null; // 消息发送到的队列rn rn Queue iQueue = null; // 接收消息的队列rn rn tryrn rn rn JNDIUtil jndiUtil= new JNDIUtil(icf,url);rn factory=jndiUtil.getQueueConnectionFactory("TestQM_QCF");rn System.out.println(factory.toString());rn connection = factory.createQueueConnection();//就是运行到这一句时就报了前面说的那个异常.rn rn // 启动(或重新启动)入站消息的连接地址,如果没有这个调用消息不会被接收rn rn connection.start();rn rn //表示一个非相互操作会话rn rn boolean transacted = false;rn rn session = connection.createQueueSession( transacted, Session.AUTO_ACKNOWLEDGE);rn rn oQueue= jndiUtil.getQueue("OutputTestQueue");rn rn queueSender = session.createSender(oQueue);rn rn TextMessage oMsg = session.createTextMessage();rn rn oMsg.setText("www.devx.com");rn rn // 你还可以设置其他消息属性rn rn queueSender.send(oMsg);rn rn iQueue= jndiUtil.getQueue("INQUENE");rn rn queueReceiver = session.createReceiver(iQueue);rn rn Message iMsg = queueReceiver.receive(1000);rn rn if ( iMsg != null )rn System.out.println( ((TextMessage)iMsg).getText() );rn elsern System.out.println( "No messages in queue " );rn rn rn finallyrn rn //总是释放资源rn if ( queueReceiver!= null ) rn queueReceiver.close(); rn if ( queueSender!= null )rn queueSender.close();rn if ( session!= null )rn session.close();rn if ( connection!= null )rn rn connection.close();rn rn rn rn rn rn rnrnrn[/code]rn 希望大家帮我解决. 非常感谢.! 论坛

MQJMS之间的问题

06-09

我在MQ放消息(对象消息),JMS里面接收,可接收不到对象消息,还是TextMessage消息类型,请问有可能是什么问题》rn附上MQ放消息代码rnrnpackage com.cmhit.mdb.ejb;rnrnimport com.ibm.mq.*;rnrnimport javax.naming.*;rnimport javax.jms.*;rnimport javax.naming.directory.*; // administered objectsrnrnimport java.text.SimpleDateFormat;rnimport java.util.List;rnrnimport java.util.Iterator;rnrnimport java.util.Hashtable;rnimport org.apache.log4j.Logger;rnrnrnpublic class MQPut rn //private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));rn private static Logger log = Logger.getLogger(MQPut.class);rnrn private static String hostname = "192.168.78.96";rn private static String broker = "bcg.queue.manager";//对列管理其名称rn private static String QueueName="TEST_IN";rn private static int port = 1414;rn private static String channel = "DEV.JMS.SVRCONN";rn private static int CCSID = 1381;rn private static int qsize = 0;rnrnrn rn public MQPut() rn rn rnrn public String MqPutMessage() rn //String header[] = new String[5];rn //String myFileType = "";rn java.util.Date date = new java.util.Date();rn SimpleDateFormat df = new SimpleDateFormat(rn "yyyy-MM-dd HH:mm:ss.SSS");rn //log.info("MQPUT程序开始时间" + df.format(date));rnrn try rnrn MQEnvironment.hostname = hostname;rn MQEnvironment.channel = channel;rn MQEnvironment.CCSID = CCSID;rn MQEnvironment.port = port;rn MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, //Set TCP/IP or serverrn MQC.TRANSPORT_MQSERIES);rnrn MQQueueManager qmgr = new MQQueueManager(broker);rn int openOptions = rn MQC.MQOO_INPUT_AS_Q_DEF rn | MQC.MQOO_OUTPUT rn | MQC.MQOO_INQUIRE rn | MQC.MQOO_SET_IDENTITY_CONTEXT; rn MQQueue queue = qmgr.accessQueue(QueueName,openOptions, null, null, null);rn MQMessage theMessage = new MQMessage();rn rn theMessage.priority = 9;rn rn theMessage.format = MQC.MQFMT_STRING;rn// theMessage.messageFlags = MQC.MQMF_SEGMENTATION_ALLOWED;rn// theMessage.messageFlags = MQC.MQMF_SEGMENTATION_INHIBITED ;rn// rn MQPutMessageOptions pmo = new MQPutMessageOptions();rn pmo.options =MQC.MQPMO_LOGICAL_ORDER rn | MQC.MQPMO_SET_IDENTITY_CONTEXT rn | MQC.MQPMO_SYNCPOINT; rnrn MQObject mob=new MQObject(); rn mob.setSType("INSERT");rn mob.setSTable("WEB_ROLE");rn mob.setParm1("xxx1");rn mob.setParm2("xxx2");rn mob.setParm3("xxx3");rn mob.setParm4("xxx4");rn theMessage.writeObject(mob);rn rn queue.put(theMessage, pmo);rnrn qmgr.commit();rn rn queue.close();rn qmgr.disconnect();rn rn return "";rn rn catch (Exception e) rn e.printStackTrace();rn return e.toString();rn rn rn rnrn public static void main(String args[]) throws MQException rn rn MQPut ss= new MQPut() ;rn System.out.println(ss.MqPutMessage());rnrn rnrn 论坛

没有更多推荐了,返回首页