1.概述
集成两个不同厂商的产品向来不是什么自在的活,本文试图提供一种可行的方案,集成weblogic与MQ。
我们的最终的目标是,部署在weblogic上的MDB可以自动接收到投放到MQ队列的消息,也可以通过JMS队MQ列往里投放消息。
2.集成方案
weblogic与MQ的集成方案大致有两种:
一是将MQ配置为weblogic的外部JMS服务器(foreign JMS server),第二种是weblogic的消息桥(messaging bridge)
本文中描述的是第一种方案,结构大致如下:

图画得较丑,且可能有失偏颇,仅供参考...
3.集成指引
其实本文的标题有点夸大,准确来说算是一篇step-by-step的配置指引吧,不废话了...
3.1.配置websphere MQ
3.1.1创建一个MQ队列管理器
首先创建一个测试用的MQ队列管理器,名为JustinMQ2(名字随便):

图1.1.创建队列管理器

图1.2.创建队列管理器
3.1.2创建两个本地队列application和approval:

图2.1

图2.2
完成以上步骤,我们可以通过MQ的Java API测试配置是否正确:
代码如下:
import com.ibm.mq.MQEnvironment; import com.ibm.mq.MQMessage; import com.ibm.mq.MQQueue; import com.ibm.mq.MQQueueManager; import com.ibm.mq.constants.MQConstants; /** * via MQ API * @author Justin * */ public class TestViaAPI { public static void main(String[] args) throws Exception { String qManager = "JustinQM2"; String qManagerHost = "127.0.0.1"; String queueName = "application"; MQEnvironment.channel = "SYSTEM.DEF.SVRCONN"; MQEnvironment.hostname = qManagerHost; MQQueueManager qMgr = new MQQueueManager(qManager); System.out.println("open queue"); MQQueue mqQueue = qMgr.accessQueue(queueName, MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_OUTPUT); MQMessage mqMsg = new MQMessage(); mqMsg.write("test message".getBytes()); mqQueue.put(mqMsg); System.out.println("close queue"); mqQueue.close(); System.out.println("close queue mamager"); qMgr.disconnect(); } }
如果没抛异常,基本上就成功了。当然,是否真正成功还要到刚才创建的application队列看看:

图:使用API访问队列的结果
3.2绑定到JMS
下一步就是要绑定到JMS,然后通过JMS API访问MQ的队列,步骤如下:
3.2.1我们需要添加一个JMS受管对象(JNDI上下文),为了方便测试,我们使用sun的文件JNDI。


3.2.2.创建连接工厂(QueueConnectionFactory)
创建两个连接工厂applicationFactory和approvalFactory,步骤如下:





3.2.3.创建目标(Destination)
创建两个目标:applicationJMSQueue和approvalJMSQueue,步骤如下:



然后写个程序测试一下:
import java.util.Hashtable; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; /** * * * @author Justin * */ public class TestFSJNDI { public static void main(String[] args) throws Exception { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:/C:/Users/Justin/jndi"); Context jndiContext = new InitialContext(env); QueueConnectionFactory sendFactory = (QueueConnectionFactory) jndiContext .lookup("applicationFactory"); Queue queue = (Queue) jndiContext.lookup("applicationJMSQueue"); QueueConnection queueConnection = sendFactory.createQueueConnection(); queueConnection.start(); QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); QueueSender queueSender = queueSession.createSender(queue); TextMessage textMsg = queueSession .createTextMessage("test post message via jndi"); queueSender.send(textMsg); queueConnection.close(); } }
运行后,查看队列中多出一条消息,成功。
1 楼
tiger888
2010-01-02
写的不错,赞一个