EJB---->消息驱动bean--Topic 消息的发送与接收(Pub/sub 消息传递模型)
EJB---->消息驱动bean--Topic 消息的发送与接收(Pub/sub 消息传递模型)
小强斋太 发表于1年前
EJB---->消息驱动bean--Topic 消息的发送与接收(Pub/sub 消息传递模型)
  • 发表于 1年前
  • 阅读 3
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

TopicSender.java (Topic 消息发送者)
import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;

import com.foshanshop.ejb3.TSender;
/**
* 发送Topic消息
* @author lihuoming
*
*/
@Stateless
@Remote (TSender.class)
public class TSenderBean implements TSender {
    @Resource(mappedName="TopicConnectionFactory") private TopicConnectionFactory factory;
    @Resource(mappedName="topic/chatTopic") private Topic destination;
    
public void send(String msg) {
  TopicConnection conn = null;
  TopicSession session = null;
        try {            
            conn = factory.createTopicConnection();
            session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(destination);
            TextMessage text = session.createTextMessage(msg);
            producer.send(text);
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            try {
                session.close ();
                conn.close();
            } catch (JMSException e) {
              e.printStackTrace();
            }
        }
}
}
TopicPrintBeanOne.java(Topic 消息接收者之一)
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.Topic"),
  @ActivationConfigProperty(propertyName="destination",
    propertyValue="topic/chatTopic")
})
public class TopicPrintBeanOne implements MessageListener{
    
    public void onMessage(Message msg) {
        try {            
            TextMessage tmsg = (TextMessage) msg;
            String content = tmsg.getText();
            System.out.println(this.getClass().getName()+"=="+ content);             
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
TopicPrintBeanTwo.java(Topic 消息接收者之二)
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.Topic"),
  @ActivationConfigProperty(propertyName="destination",
    propertyValue="topic/chatTopic")
})
public class TopicPrintBeanTwo implements MessageListener{

    public void onMessage(Message msg) {
        try {
            TextMessage tmsg = (TextMessage) msg;
            String content = tmsg.getText();
            System.out.println(this.getClass().getName()+"=="+ content);           
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}


 

共有 人打赏支持
粉丝 0
博文 181
码字总数 0
×
小强斋太
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: