EJB3.0+JBoss6开发JMS 配置与代码

一、配置

1.jboss5.1之前的版本是把xxxx-service.xml拷贝到\server\default\deploy\目录下,其内容如下:

 

<?xml version="1.0" encoding="UTF-8"?>

<server>  

  <mbean code="org.jboss.mq.server.jmx.Queue"

         name="jboss.mq.destination:service=Queue,name=myQueue">

    <attribute name="JNDIName">queue/myQueue</attribute>   

    <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>

  </mbean>

  <mbean code="org.jboss.mq.server.jmx.Topic"

name="jboss.mq.destination:service=Topic,name=myTopic">

<attribute name="JNDIName">topic/myTopic</attribute>

    <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>

  </mbean>

</server>

 

2. Jboss6配置: 在jboss-6.1.0.Final\server\all\deploy\hornetq\hornetq-jms.xml 加入配置

 

<queue name="myQueue">

       <entry name="/queue/myQueue"/>

   </queue>

   <topic name="myTopic">

       <entry name="/topic/myTopic"/>

   </topic>

二、代码

1.jboss5.1之前的版本的连接工厂JNDI分别为:TopicConnectionFactory和QueueConnectionFactory

在Session Bean中代码:

 

@Stateless

@Remote (TSender.class)

public class TSenderBean implements TSender {

    @Resource(mappedName="QueueConnectionFactory") private QueueConnectionFactory factory;

    @Resource(mappedName="queue/myQueue") private Queue destination;

    //@Resource(mappedName="TopicConnectionFactory") private TopicConnectionFactory factory;

    //@Resource(mappedName="topic/myTopic") private Topic destination;

    

public void send(String msg) {

TopicConnection conn = null;

TopicSession session = null;

        try {

            conn = factory.createQueueConnection();

            session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

            MessageProducer producer = session.createProducer(destination);

            //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();

            }

        }

}

}

 

 

在java类中代码:

 

 Properties props = new Properties();

            props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

            props.setProperty(Context.PROVIDER_URL, "localhost:1099");

            props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

            InitialContext ctx = new InitialContext(props);

            QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");  //PTP(点对点) 消息传递模型规定了一条消息只能传递给一个接收方

            //TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("TopicConnectionFactory"); //Pub/sub (发布和订阅)消息传递模型允许一条消息传递给多个接收方

           //TopicConnection conn = factory.createTopicConnection();

           //TopicSession session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);

           //Destination destination = (Topic) ctx.lookup("topic/myTopic");

            TopicConnection conn = factory.createQueueConnection();

            TopicSession session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

            Destination destination = (Queue) ctx.lookup("queue/myQueue");

         

            MessageProducer producer = session.createProducer(destination);

            //发送文本

            TextMessage msg = session.createTextMessage("Hello Bean");

            producer.send(msg);

2.Jboss6中  QueueConnectionFactory和QueueConnectionFactory的连接工厂JNDI合并成了ConnectionFactory,  so 把上面代码连接工厂JNDI改为ConnectionFactory就OK了 

 

 

你可能感兴趣的:(JBOSS6配置JMX,EJB配置JMX)