• Ejb消息驱动bean


    一 点对点发送消息:

    首先配置文件XX_service.xml放到 到jboss-4.2.2.GA\server\default\deploy中

    配置代码如下:

    <server>  
      <mbean code="org.jboss.mq.server.jmx.Queue"
             name="jboss.mq.destination:service=Queue,name=xiaoyi">
        <attribute name="JNDIName">queue/xiaoyi</attribute>   
        <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
      </mbean>
    </server>

    新建一ejb工程,建一工具类(主要用来加载context)代码如下:

    public static Context getcontext(){
            
            Context cx = null;
                    Properties pro = new Properties();
            pro.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                    "org.jnp.interfaces.NamingContextFactory");
            pro.setProperty(Context.PROVIDER_URL, "localhost:1099");
            try {
                cx = new InitialContext(pro);
            } catch (NamingException e) {
                e.printStackTrace();
            }
            return cx;   
        }  

     建一消息发送类;发送代码如下:

    Context cx = null; //用来获得应用上下文  
            try {
                cx = JDBIUtil.getcontext();//调用工具文件,加载文本信息,连接到服务器,
                QueueConnectionFactory connfactory = (QueueConnectionFactory) cx.lookup("QueueConnectionFactory");// 获得链接工厂
                
                QueueConnection conn = connfactory.createQueueConnection();// 获得链接
                
                QueueSession qs = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);//创建会话
                
                Destination ds = (Destination) cx.lookup("queue/xiaoyi");//获得消息发送目的地
                
                TextMessage tm = qs.createTextMessage("测试信息"); //发送的内容
                
                MessageProducer mp = qs.createProducer(ds); //发送消息
                mp.send(tm);
                System.out.println("发送消息.....");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
    }

    监听消息,用来接收消息,新建一个EJB MESSAGE DRIVE BEAN,代码如下:
    package org.pxw.util;
    
    import javax.ejb.ActivationConfigProperty;
    import javax.ejb.MessageDriven;
    import javax.jms.Message;
    import javax.jms.MessageListener;
    import javax.jms.TextMessage;
    
    @MessageDriven(mappedName = "jms/MDBTest", activationConfig = {
            @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
            @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
            @ActivationConfigProperty(propertyName="destination",propertyValue="queue/xiaoyi")//消息地址
    })
    public class MDBTest implements MessageListener {
    
        @Override
        public void onMessage(Message msg) {
            // TODO Auto-generated method stub
    
            TextMessage tm = (TextMessage) msg;
            
            System.out.println("的到的值:"+tm);
            
        }
    
    }
    二 消息订阅
    同上在XX-service.xml中加上如下配置代码:
     <mbean code="org.jboss.mq.server.jmx.Topic"
         name="jboss.mq.destination:service=Topic,name=pxw">
        <attribute name="JNDIName">topic/pxw</attribute>
        <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
      </mbean>

    消息发送类的代码:

    Context cx = null;
            
            cx = Jndiutil.getcontext();
            
            try {
                TopicConnectionFactory  connFactory = (TopicConnectionFactory) cx.lookup("TopicConnectionFactory");
                
                TopicConnection conn = connFactory.createTopicConnection();
                
                Destination destination = (Destination) cx.lookup("topic/pxw");
                
                TopicSession session = conn.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE);
                
                TextMessage tm = session.createTextMessage("topic.Test测试内容");
                
                MessageProducer mp = session.createProducer(destination);
                
                mp.send(tm);
                
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            

    消息接收类的代码,注意在创建EJB MESSAGE DRIVER BEAN时 Destination Type选择topic:

    import javax.ejb.ActivationConfigProperty;
    import javax.ejb.MessageDriven;
    import javax.jms.Message;
    import javax.jms.MessageListener;
    import javax.jms.TextMessage;
    
    @MessageDriven(mappedName = "jms/TopidMDB", activationConfig = {
            @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
            @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
            @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
            @ActivationConfigProperty(propertyName = "clientId", propertyValue = "TopidMDB"),
            @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "TopidMDB"),
            @ActivationConfigProperty(propertyName="destination",propertyValue="topic/pxw")
    })
    public class TopidMDB implements MessageListener {
    
        @Override
        public void onMessage(Message message) {
            // TODO Auto-generated method stub
            
            TextMessage tx = (TextMessage) message;
            
            System.out.println(tx);
        }
    
    }

     

     
  • 相关阅读:
    Salesforce 大数据量处理篇(一)Skinny Table
    salesforce零基础学习(九十九)Salesforce Data Skew(数据倾斜)
    将博客搬至CSDN,新地址见此文章内容
    Spring Boot
    SpringBoot的模块及结构
    不建议现场注入– Spring IOC
    程序员健康指南
    如何使用Chrome DevTools花式打断点
    这堂课一上40年, 他告诉学生:就算你进了麻省理工, 不懂这个也很难成功!
    非对称加密算法--RSA加密原理
  • 原文地址:https://www.cnblogs.com/b422/p/ejb_test.html
Copyright © 2020-2023  润新知