如何在Wildfly上搭建Ibm mq的消息队列

原创 2013年12月06日 09:45:10

说到Ibm mq消息队列,我想用过WebSphere服务器的人都不陌生,也能很熟练的在Was上配置一个队列服务,但是如何在Wildfly(Jboss前身)上配置Ibm mq的消息队列,网上给的资料也少,笔者了解过Wildfly内置的消息中间件是Hornetq,要想在Wildfly应用服务器上使用mq消息中间件,那还得做些配置,下面笔者来介绍下如何在Wildfly上搭建ibm mq消息队列。

步骤一:下载wmq.jmsra-7.0.1.3.rar,放到wildfly安装目录standalone/deployments下面;

步骤二:更改standalone目录下standalone-full.xml的配置,找到如下标签<subsystem xmlns="urn:jboss:domain:resource-adapters:2.0"/>,在标签下添加如下配置:

<subsystem xmlns="urn:jboss:domain:resource-adapters:2.0">
            <resource-adapters>
                <resource-adapter id="wmq.jmsra-7.0.1.3.rar">
                    <archive>
                        wmq.jmsra-7.0.1.3.rar
                    </archive>
                    <transaction-support>NoTransaction</transaction-support>
                    <connection-definitions>
                        <connection-definition class-name="com.ibm.mq.connector.outbound.ManagedConnectionFactoryImpl" jndi-name="java:jboss/ManagedConnectionFactoryImpl" enabled="true" pool-name="ManagedConnectionFactoryImpl">
                            <config-property name="port">
                                1420
                            </config-property>
                            <config-property name="hostName">
                                127.0.0.1
                            </config-property>
                            <config-property name="channel">
                                CHANNEL1
                            </config-property>
                            <config-property name="CCSID">
                                1208
                            </config-property>
                            <config-property name="queueManager">
                                APPDEV01_QM
                            </config-property>
                            <security>

                              <!--不用身份验证,也就不需要在WildFly上添加application的user用户-->
                                <application>false</application>

                            </security>
                            <validation>
                                <background-validation>false</background-validation>
                            </validation>
                        </connection-definition>
                    </connection-definitions>
                    <admin-objects>
                        <admin-object class-name="com.ibm.mq.connector.outbound.MQQueueProxy" jndi-name="java:jboss/MQQueueProxy" enabled="true" use-java-context="false" pool-name="CGF_TESTQ">
                            <config-property name="baseQueueName">
                                CGF_TESTQ
                            </config-property>
                        </admin-object>
                    </admin-objects>
                </resource-adapter>
            </resource-adapters>
        </subsystem>

(注:MQ的地址根据自己的配置,生产环境下的配置谨慎使用)

另外找到<subsystem xmlns="urn:jboss:domain:ejb3:2.0">该标签的MDB配置,替换为:

             <mdb>

                <resource-adapter-ref resource-adapter-name="wmq.jmsra-7.0.1.3.rar"/>

                <bean-instance-pool-ref pool-name="mdb-strict-max-pool"/>

            </mdb>

做好上述配置之后,就要编写Java EE的程序来进行测试了,笔者代码里面用到了Java EE7的特性,所以在项目的classpath下要导入javaee7的jar包,如果读者想单独测试下mq的连通性,笔者附件项目里面也有单独测试mq 消息发送和接收的SE程序,如下是利用JMS1.X消息传递的两个java类和一个测试的servlet:

消息发送:

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.JMSRuntimeException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;

@Stateless
public class MessageSender {
	@Resource(lookup = "java:jboss/ManagedConnectionFactoryImpl")
	private ConnectionFactory factory;

	// @Resource(lookup="java:jboss/exported/jms/queue/test")
	@Resource(lookup = "java:jboss/MQQueueProxy")
	Queue myQueue;

	public void sendMessage(String message) throws JMSException {
		System.out.println("工厂对象为: " + factory.getClass().getSimpleName());
		System.out.println("队列对象为: " + myQueue.getClass().getSimpleName());
		/*
		 * try (JMSContext context =
		 * factory.createContext("producer","producer@163")) {
		 * context.createProducer().send(myQueue, message); }catch
		 * (JMSRuntimeException e) { // TODO: handle exception }
		 */

		Connection connection = null;
		try {
		/**
		  * username和password是在通过add-user.bat添加application身份的用户
		  *connection = factory.createConnection("producer","producer@163");	
		  */
                  connection = factory.createConnection();
					} catch (Exception e) {
			System.out.println("创建连接对象时出了问题");
			e.printStackTrace();
		}
		System.out.println("连接对象为: "+connection.getClass().getSimpleName());
		Session session = connection.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
		MessageProducer producer = session.createProducer(myQueue);
		connection.start();
		TextMessage textMessage = null;
		textMessage = session.createTextMessage(message);
		producer.send(textMessage);

	}
}

 

消息接收:

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;

@Stateless
public class MessageReceiver {
	@Resource(lookup="java:jboss/ManagedConnectionFactoryImpl")
    private ConnectionFactory factory;
    
	//@Resource(mappedName="java:jboss/exported/jms/queue/test")
	@Resource(lookup="java:jboss/MQQueueProxy")
    Queue myQueue;

    public String receiveMessage() {
        /*try (JMSContext context = factory.createContext("producer","producer@163")) {
            return context.createConsumer(myQueue).receiveBody(String.class, 1000);
        }*/
    	System.out.println("接收端的工厂: "+factory.getClass().getSimpleName());
    	System.out.println("接收端的队列: "+myQueue.getClass().getSimpleName());
    	String rmessage = "";
    	try {
			Connection connection = factory.createConnection();
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			MessageConsumer consumer = session.createConsumer(myQueue);
			connection.start();
			TextMessage textMessage = (TextMessage) consumer.receive(5000);
			rmessage = textMessage.getText();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return rmessage;
    }
}

测试的Servlet:

import java.io.IOException;
import java.io.PrintWriter;

import javax.inject.Inject;
import javax.jms.JMSException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet(urlPatterns = {"/TestServlet"})
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Inject
    MessageSender sender;

    @Inject
    MessageReceiver receiver;   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String message = "你好,世界!";
		request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Application Managed JMSContext</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Application Managed JMSContext</h1>");
        try {
			sender.sendMessage(message);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("发送消息出了异常。。。。");
			return;
		}
        out.println("Message sent: " + message + "<br><br>");
        out.println("Message received: " + receiver.receiveMessage());
        out.println("</body>");
        out.println("</html>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

以读取standalone-full.xml配置的方式启动服务器,访问servlet,查看MQ的消息发送和接收情况,可看到如下页面:

相关文章推荐

jboss配置使用JMS消息队列

 在JBoss服务器上创建消息队列 我们可以使用如下4种方式创建消息队列: Management ConsoleManagement CLI部署*-jms.xml 文件到 deploym...

JBOSS集成的ActiveMQ处理消息

上篇博客对JBOSS和ActiveMQ集成做了一下实现,这篇用一个DEMO来看如何来用两者集成之后的ActiveMQ来处理消息。 这个DEMO是用STRUTS2+Spring3+MDB实现的,主要的功...
  • lfsf802
  • lfsf802
  • 2013年05月15日 09:50
  • 3201

wildfly-9.0.2 web项目部署详细步骤

一、配置操作系统环境变量       JAVA_HOME = C:\Program Files (x86)\Java\jdk1.7.0_67       JBOSS_HOME = F:\s...

Wildfly domain多机多节点集群方式搭建

准备工作如下:               物理机os   win7    ip 10.22.8.237  (主机domain  controller)               虚拟linux系统...

JBoss页面部署消息队列及相关例子

一、download JBoss-EAP-6.2.0GA: http://jbossas.jboss.org/downloads JBoss Enterprise Application Platf...

配置Wildfly需要注意的地方

Wildfly使用过程中用到的问题若创建的wildfly项目中使用到了MySql数据库,则需要在standalone.xml文件中引入MySQL驱动: //module值可自定义。 在modules文...

wildfly8.1部署注意事项

服务器改成支持外网访问 在standalone.xml文件中找到             在这个下面加入                 另外把1改成2 1   2   ...

Struts2+Ueditor配置和图片上传

这几天正在做我们专业的网站(笔者是一个学生),里面有新闻发布的功能,于是就与Ueditor打上了交道。但是问题也随之而来了,Ueditor图片上传不上去,编辑的图片不能用。然后就在网上找资料,看到大量...

Struts2中利用filter、session实现安全访问和身份认证

1、开发环境: Eclipse软件 JDK 1.7 Apach Tomcat 7 2、通过eclipse创建Dynamic Web Project后,导入相应的Struts2 的jar文件: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何在Wildfly上搭建Ibm mq的消息队列
举报原因:
原因补充:

(最多只允许输入30个字)