关闭

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

标签: Wildfly WebSphereMQJMSJava EE7
1696人阅读 评论(0) 收藏 举报
分类:

说到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的消息发送和接收情况,可看到如下页面:

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15331次
    • 积分:241
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    最新评论