消息驱动bean的使用

  1. 通过buildPath把jboss中client中的所有jar包导入
  2. 用于接收消息的类
    package revieve;
    
    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/myQueue"),
    		@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
    public class recieve implements MessageListener {
    
    	@Override
    	public void onMessage(Message arg0) {
    		// TODO Auto-generated method stub
    		TextMessage tm = (TextMessage) arg0;
    		try {
    			System.out.println(tm.getText());
    		} catch (JMSException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    }
    

    @MessageDriven中的destination propertyValue自定义

  3. 发送消息的类

    package send;
    
    import javax.jms.ConnectionFactory;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.MessageProducer;
    import javax.jms.QueueConnection;
    import javax.jms.QueueSession;
    import javax.jms.TextMessage;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    
    public class sender {
    
    	/**
    	 * @param args
    	 * @throws NamingException
    	 * @throws JMSException
    	 */
    	public static void main(String[] args) throws NamingException, JMSException {
    		// TODO Auto-generated method stub
    
    		Context ctx = new InitialContext();
    		ConnectionFactory cf = (ConnectionFactory) ctx
    				.lookup("ConnectionFactory");
    		QueueConnection qc = (QueueConnection) cf.createConnection();
    		QueueSession session = qc.createQueueSession(false,
    				QueueSession.AUTO_ACKNOWLEDGE);
    		Destination destination = (Destination) ctx.lookup("queue/myQueue");
    		MessageProducer producer = session.createProducer(destination);
    		TextMessage tm = session.createTextMessage("Hello,消息驱动bean!");
    		producer.send(tm);
    		System.out.println("Test passes!");
    		session.close();
    		qc.close();
    	}
    
    }
    

    ctx.lookup()查找的destination需要与上文中的一致

  4. 编写build.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project name="message" default="ejbjar" basedir=".">
      
      <property name="src.dir" value="${basedir}\src" />
    	<property environment="env" />
    	<property name="jboss.home" value="${env.JBOSS_HOME}" />
    	<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}" debug="on" encoding="UTF-8">
    			
    			<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>
    		</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>
    
  5. 打包成jar,放到 jboss/server/default/deploy 目录下(jboss5)

  6. 把刚刚的 jar 包导入项目中

  7. 编写 **-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=myQueue">
        <attribute name="JNDIName">queue/myQueue </attribute>   
        <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
      </mbean>
    </server>

    jndiName就是上面ctx.lookup()查找的内容,必须一致

  8. 在类中加入jndi.properties文件(或者测试类中初始上下文的时候写进去)

    java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
    java.naming.provider.url=localhost\:1099
  9. 通过send类发送一个消息,如果部署成功,会自动输出

发布了143 篇原创文章 · 获赞 11 · 访问量 8202

猜你喜欢

转载自blog.csdn.net/weixin_43701790/article/details/103591863
今日推荐