Wildfly10 部署MDB

版权声明:本文为博主原创文章,非商用转载请注明出处: https://blog.csdn.net/qq_17058993/article/details/83825421

原文链接:jboss7部署MDB

原文是针对JBoss7的,配置文件已经不适用,最新的Wildfly10使用 standalone-ful.xml 根据原文稍微修改下代码就可以

EJB的第二种类型:

Message Driven Bean,MDB:用于Java EE应用程序中各组件之间的异步通信,可用于接收与Java消息传递服务(JMS)兼容的消息,并根据接收消息的内容采取一些操作。

消息驱动Bean(MDB)使Java EE应用程序异步处理消息。一旦部署在应用程序服务器上,它将监听JMS消息,并为每个收到的消息执行一个操作(调用MDB的onMessage方法)。 MDB为应用程序开发提供事件驱动的松散耦合模型。 MDB不会被注入或从客户端代码中调用,但会由收到的消息触发。

MDB是无状态的,不与客户保持任何对话状态。应用程序服务器维护一个MDB池,并通过分配和返回池中的实例并管理它们的生命周期。他们也可以参与事务处理,并且应用程序服务器根据消息处理的结果负责消息重新传送和消息接收确认。

有许多可以使用MDB的用例。最流行的是解耦系统,并防止它们的API通过直接调用而被紧密耦合。相反,两个系统可以通过以异步方式传递消息来进行通信,这确保了两个系统可以独立进化而不会相互影响。

原文链接:https://cloud.tencent.com/developer/article/1151941


一、standalone-ful.xml

1、Wildfly启动默认使用的是 standalone.xml ,但是这个版本的standalone.xml 中没有MDB的配置,但是standalone-ful.xml 中有

2、如果根据原文链接博客的修改standalone.xml,会报各种错误,因为版本不一致。

3、还是默认使用 standalone.xml 这里不修改, 直接copy standalone-ful.xml到 standalone.xml

扫描二维码关注公众号,回复: 4185669 查看本文章

文件路径:wildfly-10.1.0.Final\standalone\configuration 

二、Creating New EJB Project

1、Eclipse创建EJB  FirstMDBProject

2、Creating object message class

Employee  作为消息传递的实体

package com.theopentutorials.mdb.to;
import java.io.Serializable;

public class Employee implements Serializable {

	private int id;
	private String name;
	private String designation;
	private double salary;

	public Employee() {
	}
	public Employee(int id, String name, String designation, double salary) {
		this.id = id;
		this.name = name;
		this.designation = designation;
		this.salary = salary;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDesignation() {
		return designation;
	}
	public void setDesignation(String designation) {
		this.designation = designation;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", designation=" + designation + ", salary=" + salary + "]";
	}
}

三、Creating Message Driven Bean Consumer

1、ejbModule -> New -> Message-Driven Bean (EJB 3.x)

  • Enter the Java package name as com.theopentutorials.mdb
  • Enter the Class name as QueueListenerMDB
  • Select the Destination type as Queue
  • Click “Finish

2、最终的项目结构:

3、自动生成 QueueListenerMDB.java 

package com.theopentutorials.mdb;
import java.util.Date;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;
import com.theopentutorials.mdb.to.Employee;
/**
 * Message-Driven Bean implementation class for: QueueListenerMDB
 */
@MessageDriven(activationConfig = {
		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
		@ActivationConfigProperty(propertyName = "destination", propertyValue = "java:/jms/queue/ExpiryQueue") })
public class QueueListenerMDB implements MessageListener {
	/**
	 * Default constructor.
	 */
	public QueueListenerMDB() {
	}
	/**
	 * @see MessageListener#onMessage(Message)
	 */
	public void onMessage(Message message) {

		try {

			if (message instanceof TextMessage) {
				System.out.println("Queue: I received a TextMessage at " + new Date());

				TextMessage msg = (TextMessage) message;

				System.out.println("Message is : " + msg.getText());

			} else if (message instanceof ObjectMessage) {
				System.out.println("Queue: I received an ObjectMessage at " + new Date());

				ObjectMessage msg = (ObjectMessage) message;
				Employee employee = (Employee) msg.getObject();

				System.out.println("Employee Details: ");
				System.out.println(employee.getId());
				System.out.println(employee.getName());
				System.out.println(employee.getDesignation());
				System.out.println(employee.getSalary());

			} else {
				System.out.println("Not a valid message for this Queue MDB");
			}
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}

第二个@ActivationConfigProperty 没有需要自己添加上 

wildfly-10.1.0.Final\standalone\configuration 中的 standalone.xml 没有MDB相关配置,需要拷贝 standalone-ful.xml 

然后和之前修改的standalone.xml同步

  • The activationConfig property of @MessageDriven is an array of ActivationConfigProperty and it should specify the destinationType (Queue or Topic) and destination (Queue/Topic’s JNDI name).

查看自己的 standalone.xml(也就是standalone-ful.xml)发现 Queue’s JNDI name :java:/jms/queue/ExpiryQueue

在 @ActivationConfigProperty 中进行配置 

四、Creating JMS client (Dynamic Web Project)

1、就是创建Web工程 MDBWebClient

2、Creating Servlet class

 ServletMessageProducer.class

3、将 EJB工程中的实体 Employee 打成jar 放到 WEB工程的 WebContent-WEB-INF-lib 下 自动build 

这里用到JMS传递消息的第二种类型:ObjectMessage 

4、Queue查找JNDI名字,和配置文件中的名字对应起来:java:/jms/queue/ExpiryQueue

package com.theopentutorials.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.theopentutorials.mdb.to.Employee;

@WebServlet("/ServletMessageProducer")
public class ServletMessageProducer extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ServletMessageProducer() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// response.getWriter().append("Served at: ").append(request.getContextPath());

		// final String QUEUE_LOOKUP = "queue/MyQueue";
		final String QUEUE_LOOKUP = "java:/jms/queue/ExpiryQueue";
		final String CONNECTION_FACTORY = "ConnectionFactory";

		PrintWriter out = response.getWriter();

		try {

			Context context = new InitialContext();
			QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup(CONNECTION_FACTORY);
			QueueConnection connection = factory.createQueueConnection();
			QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

			Queue queue = (Queue) context.lookup(QUEUE_LOOKUP);
			QueueSender sender = session.createSender(queue);

			// 1. Sending TextMessage to the Queue
			TextMessage message = session.createTextMessage();
			message.setText("Hello EJB3 MDB Queue!!!");
			sender.send(message);
			
			out.println("1. Sent TextMessage to the Queue");

			
			//2. Sending ObjectMessage to the Queue
			ObjectMessage objMsg = session.createObjectMessage();
			
			Employee employee = new Employee();
			employee.setId(2018);
			employee.setName("wls");
			employee.setDesignation("CTO");
			employee.setSalary(10000);
			
			objMsg.setObject(employee);
			sender.send(objMsg);
			
			out.println("2. Sent ObjectMessage to the Queue");
			
			
			session.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

五、Deploying Dynamic Web Project 、EJB project

1、在Wildfly部署 EJB。WEB

2、浏览器访问: http://localhost:8081/MDBWebClient/ServletMessageProducer

3、console

猜你喜欢

转载自blog.csdn.net/qq_17058993/article/details/83825421
今日推荐