J2EE中的JMS 消息服务

下面就java中的消息服务作如下学习总结:
java中的消息服务有两种分别是:Queen和Topic
步骤如下:无论是那种消息都需要先配置目标地址文件:message-service.xml
(*-service.xml)*表示你的命名后面必须一致
准备工作:
<1、导入*\jboss-4.2.3.GA-jdk6\jboss-4.2.3.GA\client
中的所有*.jar文件
<2、使用ANT来编译和发布EJB build.xml 内容如下:
<?xml version="1.0"?>

<!-- ======================================================================= -->
<!-- EJB3 HelloWorld build file                                                       -->
<!-- ======================================================================= -->

<project name="MessageDriverBean" default="ejbjar" basedir=".">

<property environment="env" />
<property name="app.dir" value="${basedir}" />
<property name="src.dir" value="${app.dir}\src" />
<property name="jboss.home" value="${env.JBOSS_HOME}" />
<property name="jboss.server.config" value="default" />
<property name="build.dir" value="${app.dir}\build" />
<property name="build.classes.dir" value="${build.dir}\classes" />

<!-- Build classpath -->
<path id="build.classpath">
<fileset dir="${jboss.home}\client">
<include name="*.jar" />
</fileset>
<pathelement location="${build.classes.dir}" />
</path>

<!-- =================================================================== -->
<!-- Prepares the build directory                                        -->
<!-- =================================================================== -->
<target name="prepare" depends="clean">
<mkdir dir="${build.dir}" />
<mkdir dir="${build.classes.dir}" />
</target>

<!-- =================================================================== -->
<!-- Compiles the source code                                            -->
<!-- =================================================================== -->
<target name="compile" depends="prepare" description="编绎">
<javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" deprecation="on" optimize="off" includes="com/**">
<classpath refid="build.classpath" />
</javac>
</target>

<target name="ejbjar" depends="compile" description="创建EJB发布包">
<jar jarfile="${app.dir}\HelloWorld.jar">
<fileset dir="${build.classes.dir}">
<include name="com/**/*.class" />
</fileset>
</jar>
</target>

<target name="deploy" depends="ejbjar">
<copy file="${app.dir}\HelloWorld.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy" />
</target>

<!-- =================================================================== -->
<!-- Cleans up generated stuff                                           -->
<!-- =================================================================== -->
<target name="clean">
<delete dir="${build.dir}" />
<delete file="${jboss.home}\server\${jboss.server.config}\deploy\HelloWorld.jar" />
</target>

</project>



Queen:
第一步:配置message-service.xml文件内容如下:然后发布到Jboss中

<?xml version="1.0" encoding="UTF-8"?>
<server> 
  <mbean code="org.jboss.mq.server.jmx.Queue"
  name="jboss.mq.destination:service=Queue,name=liyong.com">
<attribute name="JNDIName">queue/liyong.com</attribute>  
    <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
  </mbean>
第二步:编译和发布message-service.xml拷贝到*\jboss-4.2.3.GA-jdk6\jboss-4.2.3.GA\server\default\deploy
第三步:编写EJB Bean发送消息
public static void main(String[] args) {

try {
InitialContext initialContext=new InitialContext();
QueueConnectionFactory factory = (QueueConnectionFactory)initialContext.lookup("QueueConnectionFactory");
Connection con=factory.createConnection();
//第一个参数:是否建立事务
//第二个参数:表示消息确认模式
Session session=con.createSession(false,QueueSession.AUTO_ACKNOWLEDGE);
//查找目标地址
Destination destination = (Destination )initialContext.lookup("queue/liyong.com");
//创建发送消息
MessageProducer producer = session.createProducer(destination);
TextMessage msg = session.createTextMessage("您好,这是我的第一个消息驱动QueueBean");
producer.send(msg);
//关闭相应资源
producer.close();
session.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
第四步:编写接受端代码:采用消息驱动Bean (Message Driven Bean)接收消息
@MessageDriven(activationConfig =
{
  @ActivationConfigProperty(propertyName="destinationType",
    propertyValue="javax.jms.Queue"),
  @ActivationConfigProperty(propertyName="destination",
    propertyValue="queue/liyong.com"),
  @ActivationConfigProperty(propertyName="acknowledgeMode",
    propertyValue="Auto-acknowledge")
})
public class MessageDriverBeanQuee implements MessageListener{

//第一步:注册这个消息驱动监听位置
//第二步:接受消息
//第三步:发布到服务器
@Override
public void onMessage(Message message) {
TextMessage textMessage=(TextMessage)message;
try {
System.out.println(textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}

}
第五步:发布EJB

Topic:

准备工作:如上

第一步:
配置目标地址文件
<?xml version="1.0" encoding="UTF-8"?>
<server> 
  <mbean code="org.jboss.mq.server.jmx.Queue"
  name="jboss.mq.destination:service=Queue,name=liyong.com">
<attribute name="JNDIName">queue/liyong.com</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=liyong.com">
<attribute name="JNDIName">topic/liyong.com</attribute>  
    <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
  </mbean>
</server>
第二步:编译和发布message-service.xml拷贝到*\jboss-4.2.3.GA-jdk6\jboss-4.2.3.GA\server\default\deploy
第三步:编写EJB Bean发送消息
public static void main(String[] args) {

try {
InitialContext initialContext=new InitialContext();
TopicConnectionFactory factory = (TopicConnectionFactory)initialContext.lookup("TopicConnectionFactory");
Connection con=factory.createConnection();
//第一个参数:是否建立事务
//第二个参数:表示消息确认模式
Session session=con.createSession(false,TopicSession.AUTO_ACKNOWLEDGE);
//查找目标地址
Destination destination = (Destination )initialContext.lookup("topic/liyong.com");
//创建发送消息
MessageProducer producer = session.createProducer(destination);
TextMessage msg = session.createTextMessage("您好,这是我的第一个消息驱动TopicBean");
producer.send(msg);
//关闭相应资源
producer.close();
session.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
第四步:编写接受端代码:采用消息驱动Bean (Message Driven Bean)接收消息

这里我编写了两个Topic来接受消息
<1、
@MessageDriven(activationConfig =
{
  @ActivationConfigProperty(propertyName="destinationType",
    propertyValue="javax.jms.Topic"),
  @ActivationConfigProperty(propertyName="destination",
    propertyValue="topic/liyong.com"),
  @ActivationConfigProperty(propertyName="acknowledgeMode",
    propertyValue="Auto-acknowledge")
})
public class MessageDriverBeanTopic1 implements MessageListener{

@Override
public void onMessage(Message message) {
TextMessage textMessage=(TextMessage)message;
try {
System.out.println("MessageDriverBeanTopic1="+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
<2、
@MessageDriven(activationConfig =
{
  @ActivationConfigProperty(propertyName="destinationType",
    propertyValue="javax.jms.Topic"),
  @ActivationConfigProperty(propertyName="destination",
    propertyValue="topic/liyong.com"),
  @ActivationConfigProperty(propertyName="acknowledgeMode",
    propertyValue="Auto-acknowledge")
})
public class MessageDriverBeanTopic2 implements MessageListener{

@Override
public void onMessage(Message message) {
TextMessage textMessage=(TextMessage)message;
try {
System.out.println("MessageDriverBeanTopic2="+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}

第五步:发布EJB

大功告成....

源码至下

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