EJB3.0訊息驅動Bean

來源:互聯網
上載者:User
訊息驅動Bean(MDB)是設計用來專門處理基於訊息請求的組件。一個MDB類必須實現MessageListener 介面。當
容器檢測到bean守候的隊列一條訊息時,就調用onMessage()方法,將訊息作為參數傳入。MDB在OnMessage()
中決定如何處理該訊息。你可以用注釋來配置MDB 監聽哪一條隊列。當MDB 部署時,容器將會用到其中的注
釋資訊。
當一個業務執行的時間很長,而執行結果無需即時向使用者反饋時,很適合使用訊息驅動Bean。如訂單成功後給用
戶發送一封電子郵件或發送一條簡訊等。下面的例子在使用者下訂單完成後,列印一份配送單。好讓配送員根據地
址把商品送到客人手中。代碼如下:
PrintBean.java
//author:lihuoming
package com.foshanshop.ejb3.impl;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@MessageDriven(activationConfig =
{
@ActivationConfigProperty(propertyName="destinationType",
propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination",
propertyValue="queue/foshanshop")
})
public class PrintBean implements MessageListener {
public void onMessage(Message msg) {
try {
TextMessage tmsg = (TextMessage) msg;
this.print(tmsg.getText());
} catch (Exception e){
e.printStackTrace();
}
}
private void print(String content){
System.out.println(content);
}
}
上面通過@MessageDriven 注釋指明這是一個訊息驅動Bean,並使用@ActivationConfigProperty注釋配置訊息的
各種屬性,其中destinationType 屬性指定訊息的類型,訊息有兩種類型topics 和queues,下面是這兩種訊息
類型的介紹:
Jboss EJB3.0執行個體教程
著作權:黎活明
Topics 可以有多個用戶端。用topic 發布允許一對多,或多對多通訊通道。訊息的產生者被叫做publisher, 消
息接受者叫做subscriber。destinationType 屬性對應值:javax.jms.Topic
Queue 僅僅允許一個訊息傳送給一個客戶。一個寄件者將訊息放入訊息佇列,接受者從隊列中抽取並得到訊息,
訊息就會在隊列中消失。第一個接受者抽取並得到訊息後,其他人就不能再得到它。destinationType屬性對應
值:javax.jms.Queue
destination屬性用作指定訊息路徑,訊息驅動Bean在發布時,如果路徑不存在,容器會自動建立該路徑,當容
器關閉時該路徑會自動被刪除。
運行本例子,當一個訊息到達queue/foshanshop 隊列時,就會觸發onMessage 方法,訊息作為一個參數傳入,
在onMessage 方法裡面得到訊息體並調用print 方法把訊息內容列印到控制台上。
MessageDrivenBeanTest.jsp
<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="javax.naming.*, java.text.*, javax.jms.*, java.util.Properties"%>
<%
QueueConnection cnn = null;
QueueSender sender = null;
QueueSession sess = null;
Queue queue = null;
try {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
InitialContext ctx = new InitialContext(props);
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
cnn = factory.createQueueConnection();
sess = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup("queue/foshanshop");
} catch (Exception e) {
out.println(e.getMessage());
}
TextMessage msg = sess.createTextMessage("佛山人您好,這是我的第一個訊息驅動Bean");
sender = sess.createSender(queue);
sender.send(msg);
sess.close ();
out.println("訊息已經發送出去了,你可以到JBoss控制台查看Bean的輸出");
%>
上面的JSP用戶端用來向queue/foshanshop訊息佇列發送一條訊息。用戶端發送訊息一般有以下步驟:
(1) 得到一個JNDI初始化上下文(Context);
例子對應代碼:
Properties props = new Properties();
Jboss EJB3.0執行個體教程
著作權:黎活明
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
InitialContext ctx = new InitialContext(props);
(2) 根據上下文來尋找一個串連工廠TopicConnectFactory/ QueueConnectionFactory (有兩種串連工廠,根據是
topic/queue來使用相應的類型);
例子對應代碼:
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
(3) 從串連工廠得到一個串連(Connect 有兩種[TopicConnection/ QueueConnection]);
例子對應代碼:cnn = factory.createQueueConnection();
(4) 通過串連來建立一個會話(Session);
例子對應代碼:sess = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
這句代碼意思是:建立不需要事務的並且能自動接收訊息收條的會話,在非事務Session 中,訊息傳遞的方
式有三種:
Session.AUTO_ACKNOWLEDGE :當客戶機調用的receive方法成功返回,或當MessageListenser 成功處理
了訊息,session將會自動接收訊息的收條。
Session.CLIENT_ACKNOWLEDGE :客戶機通過調用訊息的acknowledge方法來接收訊息。接收發生在
session層。接收到一個被消費的訊息時,將自動接收該session已經消費的所有訊息。例如:如果訊息的消
費者消費了10 條訊息,然後接收15 個被傳遞的訊息,則前面的10 個訊息的收據都會在這15 個訊息中被接
收。
Session.DUPS_ACKNOWLEDGE :指示session緩慢接收訊息。
(5) 尋找目的地(Topic/ Queue);
例子對應代碼:queue = (Queue) ctx.lookup("queue/foshanshop");
(6) 根據會話以及目的地來建立訊息製造者(TopicPublisher/QueueSender)和消費者(TopicSubscriber/
QueueReceiver).
例子對應代碼:
TextMessage msg = sess.createTextMessage("佛山人您好,這是我的第一個訊息驅動Bean");
sender = sess.createSender(queue);
sender.send(msg);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.