Message Driver Bean by queue or topic jms

本文介绍了一种使用Java消息服务(JMS)通过QueueMDB和TopicMDB处理消息的应用实例。QueueMDB用于直接接收消息并处理特定队列中的消息,而TopicMDB则订阅特定主题的消息,即使客户端离线也能确保消息传递。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

queue mdb

package com.test;

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

@MessageDriven(mappedName = "jms/fileQueue", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")})
public class QueueMDB implements MessageListener {

public void onMessage(Message message) {
System.out.println("run ,i am a queue mdb ejb....");

if(message instanceof TextMessage){
TextMessage tm = (TextMessage) message ;
String user;
try {
user = tm.getStringProperty("user");
System.out.println(tm.getText() + ", user : "+user);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

}

topic mdb

package com.test;

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

@MessageDriven(mappedName = "jms/fileTopic", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
@ActivationConfigProperty(propertyName = "clientId", propertyValue = "TopicMDB"),
@ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "TopicMDB") })
public class TopicMDB implements MessageListener {

public void onMessage(Message message) {
System.out.println("run ,i am a topic mdb ejb....");

if(message instanceof TextMessage){
TextMessage tm = (TextMessage) message ;
String user;
try {
user = tm.getStringProperty("user");
System.out.println(tm.getText() + ", user : "+user);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值