EJB-MessageDrivenBean -Queue/Topic 入门案例(亲测OK)

ejb入门案例(亲测OK)
运行环境:
wildFly10.x +jdk1.8

● EJB-MDB入門代码(亲测OK)
EJBproject - MDB(Queue And Topic)
deploy目录下3个文件:
1.webApp_wildFly.war
 这个是利用URL来测试下面2个EJB程的,通过访问war包里的servlet来,给某个Queue发送数据,本例中是通过"http://localhost:8080/webApp_wildFly/TestQueue01"URL来访问"queue_sendMsg_Servlet"包下的"TestQueue01"servlet的
2.EJB_queue.jar
 这个是EJB工程,它监视从"TestQueue01"队列中接受到的信息
3.EJB_topic.jar
 这个也是EJB工程,它接受来自"TestTopic01"主题的信息
 
① EJB_queue.jar 工程结构如下,新建->EJBproject->.(不会的自己百度下)
在这里插入图片描述
ReciveFrom_testQueue01.java代码

package queueDMB;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * Message-Driven Bean implementation class for: ReciveFrom_testQueue01
 */
@MessageDriven(activationConfig = {
   
		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
		@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue01") })
public class ReciveFrom_testQueue01 implements MessageListener {
   

	/**
	 * Default constructor.
	 */
	public ReciveFrom_testQueue01() {
   
	}

	@Override
	public void onMessage(Message rcvMessage) {
   
		TextMessage msg = null;
		try {
   
			if (rcvMessage instanceof TextMessage) {
   
				msg = (TextMessage) rcvMessage;
				System.out.println("Received Message from queue: " + msg.getText());
			} else {
   
				System.out.println("Not a valid message(TextMessage) for this Queue MDB");
			}
		} catch (JMSException e) {
   
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}

}

测试给“TestQueue01”队列发送信息的客户端代码:
可以将该工程发布到deployment目录下,执行Java命令执行:
jar包执行命令:“java - jar jar包名” 执行客户端代码!
(注意:打包的时候一定要包所有引用的库文件都包含进去,也就是选择"Runnable JAR file"),见下图:在这里插入图片描述

package queue_ClientApp;

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.JMSException;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class App_TestQueue01 {
   

	public static void main(String[] args) {
   

		final String QUEUE_LOOKUP = "queue/testQueue01";
		final String FACTORY = "jms/RemoteConnectionFactory";
		Context context = null;
		Connection queueConn = null;
		try {
   
			final Properties env = new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
			env.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
			env.put(Context.SECURITY_PRINCIPAL, "guest");
			env.put(Context.SECURITY_CREDENTIALS, "guest123");
//			env.put("jboss.naming.client.ejb.context", true);

			context = new InitialContext(env);
			ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup(FACTORY);
			Queue queue = (Queue) context.lookup(QUEUE_LOOKUP);
			queueConn = connectionFactory.createConnection("guest", "guest123");
			QueueSession queueSession = (QueueSession) queueConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			QueueSender queueSender = queueSession.createSender(queue);
			for (int i = 0; i < 1; i++) {
   
				TextMessage textMsg = queueSession.createTextMessage("queue example text_" + String.format("%02d", i));
				queueSender.send(textMsg);
			}
		} catch (NamingException | JMSException e) {
   
			e.printStackTrace();
		} finally {
   
			try {
   
				if (queueConn != null) {
   
					queueConn.close();
				}
				if (context != null) {
   
					context.close();
				}
			} catch (JMSException | NamingException e) {
   
				e.printStackTrace();
			}
		}
	}

}

下面是测试给“TestQueue01”队列发送信息的war包servlet代码:
访问URL:http://localhost:8080/工程名/servlet名

package queue_sendMsg_Servlet;

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

import javax.jms.JMSException;
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.naming.NamingException;
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 TestQueue01
 */
@WebServlet("/TestQueue01")
public class TestQueue01 extends HttpServlet {
   
	private static final long serialVersionUID = 2932240929345417181L;

	public TestQueue01() {
   
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
   
		response.getWriter().append("Served at: ").append(request.getContextPath() + "<br>");
		PrintWriter out = response.getWriter();

		final String QUEUE_LOOKUP = "java:jboss/exported/queue/testQueue01"; // queue/testQueue01
		final String CONNECTION_FACTORY = "ConnectionFactory";
		Context context = null;
		QueueConnection queueConn = null;
		QueueSession queueSession = null;
		try {
   
			context = new InitialContext();
			Queue queue = (Queue) context.lookup(QUEUE_LOOKUP);
			QueueConnectionFactory cf = (QueueConnectionFactory) context.lookup(CONNECTION_FACTORY);
//			queueConn = cf.createConnection("guest", "guest123");
			queueConn = cf.createQueueConnection();
			queueSession = queueConn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

			QueueSender sender = queueSession.createSender(queue);

			// 1. Sending TextMessage to the Queue
			TextMessage message = queueSession.createTextMessage();
			message.setText("Hello EJB3 MDB Queue!!!");
			sender.send(message);
			out.println("Success Sent TextMessage to the Queue");
		} catch (NamingException | JMSException e) {
   
			e.printStackTrace();
		} finally {
   
			try {
   
				if (queueConn != null) {
   
					queueConn.close();
				}
				if (context != null) {
   
					context.close();
				}
				if (queueSession != null) {
   
					queueSession.close();
				}
			} catch (JMSException | NamingException e) {
   
				e.printStackTrace();
			}
		}
	}

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

}

然后设置要送信的queue名:
在这里插入图片描述
同样的,也给下面要发送的TestTopic01设置topic:
在这里插入图片描述

下面的内容是关于MessageDrivenBean-Topic的案例(道理同Queue,不在赘述,直接上代码):
② EJB_topic.jar詳細
在这里插入图片描述
EJB_topic 代码:

package topicDMB;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * Message-Driven Bean implementation class for: ReciveFrom_testTopic01
 */
@MessageDriven(activationConfig = {
   
		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
		@ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/testTopic01") }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ejb-.xml 是 EJB (Enterprise JavaBeans) 的配置文件,用于配置 EJB 组件的属性和行为。它的具体内容取决于你的 EJB 组件的实现和需要。 以下是一个 ejb-.xml 文件的基本结构: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 3.0//EN" "http://java.sun.com/dtd/ejb-jar_3_0.dtd"> <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" version="3.0"> <!-- EJB 模块的描述 --> <description>My EJB Module</description> <!-- EJB 组件的声明 --> <enterprise-beans> <!-- Session Bean 的声明 --> <session> <!-- Session Bean 的名称 --> <ejb-name>MySessionBean</ejb-name> <!-- Session Bean 的类型 --> <ejb-class>com.example.MySessionBean</ejb-class> <!-- Session Bean 的事务属性 --> <transaction-type>Container</transaction-type> <!-- Session Bean 的声明周期 --> <stateless/> <!-- Session Bean 的 JNDI 名称 --> <ejb-local-ref> <ejb-ref-name>ejb/MySessionBeanLocal</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <local>com.example.MySessionBeanLocal</local> </ejb-local-ref> </session> <!-- Entity Bean 的声明 --> <entity> <!-- Entity Bean 的名称 --> <ejb-name>MyEntityBean</ejb-name> <!-- Entity Bean 的类型 --> <ejb-class>com.example.MyEntityBean</ejb-class> <!-- Entity Bean 的主键 --> <prim-key-class>com.example.MyEntityPK</prim-key-class> </entity> <!-- Message Driven Bean 的声明 --> <message-driven> <!-- Message Driven Bean 的名称 --> <ejb-name>MyMessageDrivenBean</ejb-name> <!-- Message Driven Bean 的类型 --> <ejb-class>com.example.MyMessageDrivenBean</ejb-class> <!-- Message Driven Bean 的消息目的地 --> <activation-config> <activation-config-property> <activation-config-property-name>destinationType</activation-config-property-name> <activation-config-property-value>javax.jms.Queue</activation-config-property-value> </activation-config-property> <activation-config-property> <activation-config-property-name>destination</activation-config-property-name> <activation-config-property-value>queue/MyQueue</activation-config-property-value> </activation-config-property> </activation-config> </message-driven> </enterprise-beans> </ejb-jar> ``` 在这个 ejb-.xml 文件中,你需要指定 EJB 模块的描述、EJB 组件的声明和各个组件的属性。这个文件使用 XML 格式编写,具体的格式和内容可以参考 EJB 规范文档和相关教程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值