EJB3.0学习总结(三)--Java消息服务(JMS)

JMS

 

步骤1.新建项目:JMS,将JBoss安装目录下的client文件夹下的包全部导入进来

 

步骤2.在项目根目录下创建Ant服务:build.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!--.表示同一目录 ..表示在上一目录-->

<project name="JMS" basedir=".">  
   <property name="src.dir" value="${basedir}\src"/>
   <property environment="env"/>
   <property name="jboss.home" value="G:\struts2\jboss-5.1.0.GA"/>
   <property name="jboss.server.config" value="default"/>
   <property name="build.dir" value="${basedir}\build"/>
    
    <path id="build.classpath">
        <fileset dir="${jboss.home}\client">
            <include name="*.jar"/>
         </fileset>
    	<pathelement location="${build.dir}"/>
    </path>


    <target name="prepare" >
       <delete dir="${build.dir}"/>
       <mkdir dir="${build.dir}"/>
    </target>


    <target name="compile" depends="prepare" description="编译">
    <javac srcdir="${src.dir}" destdir="${build.dir}">
       <classpath refid="build.classpath"/>
    </javac>
    </target>
  

    <target name="ejbjar" depends="compile" description="创建ejb发布包">
       <jar jarfile="${basedir}\${ant.project.name}.jar">
           <fileset dir="${build.dir}">
            <include name="**/*.class"/>
            </fileset>

        <!--把persistence.xml打包到META-INF目录下-->

       	<!--<metainf dir="${src.dir}\META-INF"></metainf>-->
       </jar>
    </target>

    <target name="deploy" depends="ejbjar" description="ejb发布">
       <copy file="${basedir}\${ant.project.name}.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy"/>
    </target>
                                                                                                                                                                               
    <target name="undeploy" description="卸载ejb">
       <delete file="${jboss.home}\server\${jboss.server.config}\deploy\${ant.project.name}.jar"/>
    </target>
</project>

 

 

步骤3. 创建jms-service.xml,代码如下,并将该文件放入JBoss安装目录下的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=jmsQueue">
		<attribute name="JNDIName">queue/jmsQueue</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=jmsTopic">
		<attribute name="JNDIName">topic/jmsTopic</attribute>
		<depends optional-attribute-name="DestinationManager">
			jboss.mq:service=DestinationManager
		</depends>
	</mbean>
</server>

 

 

步骤4.创建PTP 消息传递模型

 

首先创建消息发送:

 

package cn.itcast.app;

import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

public class QueueSender {
	public static void main(String[] args) {
		try {
			InitialContext ctx = new InitialContext();
			QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("XAConnectionFactory");
			QueueConnection conn = factory.createQueueConnection();
			QueueSession session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
			Destination destination = (Destination) ctx.lookup("queue/jmsQueue");
			MessageProducer producer = session.createProducer(destination);
			TextMessage msg =session.createTextMessage("Hello,这是我第一个jmsQueue");
			producer.send(msg);
			session.close();
			conn.close();
			System.out.println("消息发送完成!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

 

然后创建消息接收:

 

 

package cn.itcast.message;

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(activationConfig =
{
  @ActivationConfigProperty(propertyName="destinationType",
    propertyValue="javax.jms.Queue"),
  @ActivationConfigProperty(propertyName="destination",
    propertyValue="queue/jmsQueue"),
  @ActivationConfigProperty(propertyName="acknowledgeMode", 
    propertyValue="Auto-acknowledge")
})
public class QueueMDB implements MessageListener {

	public void onMessage(Message message) {
		TextMessage msg = (TextMessage) message;
		try {
			System.out.println(msg.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}

 

 

然后进行测试:先运行消息发送,然后Ant发布,能打印出来信息就OK了。

 

 

 

步骤5.创建Pub/sub 消息传递模型

 

首先创建消息发送:

 

package cn.itcast.app;

import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.naming.InitialContext;

public class TopicSender {
	public static void main(String[] args) {
		try {
			InitialContext ctx = new InitialContext();
			TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("XAConnectionFactory");
			TopicConnection conn = factory.createTopicConnection();
			TopicSession session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
			Destination destination = (Destination) ctx.lookup("topic/jmsTopic");
			
			MessageProducer producer = session.createProducer(destination);
			TextMessage msg =session.createTextMessage("Hello,这是我第一个jmsTopic消息");
			producer.send(msg);
			session.close();
			conn.close();
			System.out.println("消息发送完成!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

 

 

 

然后创建消息接收:

 

package cn.itcast.message;

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(activationConfig =
{
  @ActivationConfigProperty(propertyName="destinationType",
    propertyValue="javax.jms.Topic"),
  @ActivationConfigProperty(propertyName="destination",
    propertyValue="topic/jmsTopic"),
  @ActivationConfigProperty(propertyName="acknowledgeMode", 
    propertyValue="Auto-acknowledge")
})
public class TopicMDB implements MessageListener {

	public void onMessage(Message message) {
		TextMessage msg = (TextMessage) message;
		try {
			System.out.println(this.getClass()+msg.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}

 

 

package cn.itcast.message;

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(activationConfig =
{
  @ActivationConfigProperty(propertyName="destinationType",
    propertyValue="javax.jms.Topic"),
  @ActivationConfigProperty(propertyName="destination",
    propertyValue="topic/jmsTopic"),
  @ActivationConfigProperty(propertyName="acknowledgeMode", 
    propertyValue="Auto-acknowledge")
})
public class TopicMDB2 implements MessageListener {

	public void onMessage(Message message) {
		TextMessage msg = (TextMessage) message;
		try {
			System.out.println(this.getClass()+msg.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}

 

 

 

然后进行测试:先运行消息发送,然后Ant发布,能打印出来信息就OK了。

你可能感兴趣的:(java,jboss,ant,jms,ejb)