在JMS 中使用JNDI

   在META-INF中配置如下:

   Context.xml文件內容:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jms/ConnectionFactory" auth="Container"
 type="org.apache.activemq.ActiveMQConnectionFactory"
 factory="org.apache.activemq.jndi.JNDIReferenceFactory"
 brokerURL="vm://localhost"
 brokerName="localhost"
 useEmbeddedBroker="false"
 />
<Resource name="jms/Queue"
    auth="Container"
 type="org.apache.activemq.command.ActiveMQQueue"
 factory="org.apache.activemq.jndi.JNDIReferenceFactory"
    physicalName="MY.TEST.FOO"
 />
<Resource name="jms/Topic" auth="Container"
 type="org.apache.activemq.command.ActiveMQTopic"
 factory="org.apache.activemq.jndi.JNDIReferenceFactory"
    physicalName="MY.TEST.FOO.QUEUE"
 />
</Context>

 

JMS实现类:

package cn.com.vnvtrip.flex.message;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.TopicConnection;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;

/**
 *
 * @author longgangbai
 *
 */
public class FlexMessageSender implements Runnable {

 private Context envCtx = null;

 private ActiveMQConnectionFactory connectionFactory;

 private TopicConnection connection;

 private TopicSession session;

 private ActiveMQTopic destination;

 private TopicPublisher publisher;

 private Random random = new Random();

 private int i = 0;

 private void initializer() {
  Context initCtx = null;
  try {
   initCtx = new InitialContext();
   connectionFactory = (ActiveMQConnectionFactory) initCtx
     .lookup("java:comp/env/jms/ConnectionFactory");
   System.out.println("connectionFactory =" + connectionFactory);
   destination = (ActiveMQTopic) initCtx
     .lookup("java:comp/env/jms/Topic");
   System.out.println("destination =" + destination);
   connection = connectionFactory.createTopicConnection();

   session = connection.createTopicSession(false,
     Session.AUTO_ACKNOWLEDGE);
   publisher = session.createPublisher(destination);
   connection.start();
  } catch (NamingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (JMSException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 public void send() {
  try {
   TextMessage message = session.createTextMessage();
   String value = String.valueOf(random.nextInt(100));
   message.setText(value);
   publisher.publish(message);
   System.out.println("send " + value + " ok.");
  } catch (JMSException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 public void sendObj() {
  try {
   SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
   String time = simpleDateFormat.format(new Date());
   String index = String.valueOf(i++);
   String x = String.valueOf(random.nextInt(100));
   OrderVO bean = new OrderVO();
   bean.setId(index);
   bean.setName(time);
   bean.setValue(x);
   ObjectMessage objectMessage = session.createObjectMessage();
   objectMessage.setObject(bean);
   publisher.send(objectMessage);
   System.out.println("send obj x=" + x + "-------------" + time);
  } catch (JMSException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 /*
  * (non-Javadoc)
  *
  * @see java.lang.Runnable#run()
  */
 public void run() {
  try {
   initializer();
   connection.start();
   send();
   while (true) {
    sendObj();
    // int qq = random.nextInt(10);
    Thread.sleep(1000);
   }
  } catch (JMSException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

}
web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>jmsServlet</servlet-name>
    <servlet-class>cn.com.vnvtrip.flex.message.JMSMessageServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>jmsServlet</servlet-name>
    <url-pattern>/jmsServlet</url-pattern>
  </servlet-mapping>
 
  <description>
   
  </description>
   <resource-ref>
      <description>topic/MyTopic</description>
      <res-ref-name>jms/topic/MyTopic</res-ref-name>
      <res-type>org.apache.activemq.command.ActiveMQTopic</res-type>
      <res-auth>Container</res-auth>
   </resource-ref>
  
     <description>
   
  </description>
   <resource-ref>
      <description>ConnectionFactory</description>
      <res-ref-name>jms/ConnectionFactory</res-ref-name>
      <res-type>org.apache.activemq.ActiveMQConnectionFactory</res-type>
      <res-auth>Container</res-auth>
   </resource-ref>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

使用一個Servlet测试JMS

 

package cn.com.vnvtrip.flex.message;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JMSMessageServlet extends HttpServlet {

 /**
  * Constructor of the object.
  */
 public JMSMessageServlet() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }
   /**
    * 测试JMS
    */
 public void service(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  FlexMessageSender meesagesender = new FlexMessageSender();
  Thread thread = new Thread(meesagesender);
  thread.start();
  out.println("flex message Thread ....");
  out.flush();
  out.close();
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException
  *             if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值