锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲av第一网站久章草,亚洲av无码乱码国产精品fc2,亚洲网站免费观看http://m.tkk7.com/stevenjohn/category/52113.html閭d簺闈掓槬鐨勫瞾鏈?/description>zh-cnSun, 26 Aug 2012 19:19:15 GMTSun, 26 Aug 2012 19:19:15 GMT60Apache ActiveMQ or Spring 浣跨敤鏂規硶http://m.tkk7.com/stevenjohn/archive/2012/08/26/386311.htmlabinabinSun, 26 Aug 2012 12:18:00 GMThttp://m.tkk7.com/stevenjohn/archive/2012/08/26/386311.htmlhttp://m.tkk7.com/stevenjohn/comments/386311.htmlhttp://m.tkk7.com/stevenjohn/archive/2012/08/26/386311.html#Feedback0http://m.tkk7.com/stevenjohn/comments/commentRss/386311.htmlhttp://m.tkk7.com/stevenjohn/services/trackbacks/386311.htmlApache ActiveMQ綆浠嬶細 MQ鏄疉pache鍑哄搧鐨勪竴涓熀浜嶫MS娑堟伅澶勭悊鏈哄埗鐨勬湇鍔★紝鍙互寰堝鏄撶殑娼滃叆鍒癝pring涓紝浣滀負欏圭洰娑堟伅澶勭悊鏈嶅姟銆?/p>

Apache ActiveMQ鐢ㄩ旓細 jms鏈変袱縐嶆秷鎭満鍒朵竴縐嶆槸娑堟伅闃熷垪錛屼竴縐嶆槸娑堟伅璁㈤槄錛岀畝鍗曟潵璇村氨鏄竴瀵逛竴錛屽拰涓瀵瑰銆?/p>

涓瀵逛竴鍙互鐪嬪仛 涓緇勬秷鎭搴斾竴涓?鎴?澶氫釜娑堟伅鑾峰彇鏈嶅姟錛屽叾涓瘡涓涓崟鐙殑娑堟伅浣撻兘鍙細琚竴涓秷鎭幏鍙栨湇鍔¤幏鍙栥?/p>

涓瀵瑰鍙互鐪嬪仛 涓緇勬秷鎭搴斾竴涓?鎴?澶氫釜鎺ユ敹鏈嶅姟錛屽叾涓瘡涓涓崟鐙殑娑堟伅浣撳彂鍑轟箣鍚庨兘浼氳鎵鏈夌殑鎺ユ敹鏈嶅姟鏀跺埌涓嬈°?/p>

鏍規嵁浠ヤ笂涓ょ妯″紡鎴戜滑鍙互鍦?鍙戦佺煭淇$殑鏃跺欎嬌鐢?娑堟伅闃熷垪妯″紡銆備篃鍙互鍦ㄥ垎甯冨紡鐨勬椂鍊欎嬌鐢ㄦ秷鎭槦鍒楁ā寮?/p>

娑堟伅璁㈤槄妯″紡鐢ㄧ殑涓嶆槸寰堝浣嗘槸錛屽湪鍒嗗竷寮忎腑涔熷彲浠ュ簲鐢ㄥ悓鏃跺彲浠ョ粨鍚圵ebService榪涜浣跨敤銆備緥濡備竴涓柊闂婚渶瑕佸彂緇欏涓郴緇熷唴鐨勫崟鐙湇鍔°?/p>

鍙娣卞埢鐞嗚В浜嗕粬鐨勫鐞嗘満鍒跺湪寰堝鎯呭喌涓嬮兘鍙互搴旂敤錛屽鏋滃疄鍦ㄤ笉鍠滄榪欑鏂瑰紡涔熷彲浠ラ伒鐓ava JMS鐨凙PI鐢ㄧ嚎紼嬭嚜宸辨ā鎷熸秷鎭槦鍒楁満鍒躲?/p>

鎴戜釜浜鴻涓哄尯鍒笉鏄緢澶с傛洿浣曞喌寰堝鏃跺橫Q鐨勫緢澶氬姛鑳芥垜浠湁娌℃湁鐢ㄥ埌錛岀敤榪欎箞涓ぇ瀹朵紮灝辨槸涓轟簡鍥劇渷浜嬨?/p>

榪欓噷澶囨敞涓涓嬶細鎸夊師鏈夎瀹氬湪鍒涘緩榪炴帴鐨勬椂鍊橫Q鏄笉鍙戦佹秷鎭殑銆?/span>

涓嬮潰鎴戣浠ヤ笅浣跨敤鏂瑰紡錛?/p>

鏈漢鐩墠鍙細JAVA

闇瑕佺殑鍖呬負

        activemq-all-5.2.0.jar
        log4j-1.2.14.jar
        spring-beans-2.5.5.jar
        spring-context-2.5.5.jar
        spring-jms-2.5.5.jar

         spring-core-2.5.5.jar

榪欎簺搴旇灝辮凍澶熶簡

鎴戝仛嫻嬭瘯鐨勬椂鍊欑敤鐨勫寘涓?/p>

        activemq-all-5.2.0.jar
        commons-pool-1.4.jar
        log4j-1.2.14.jar
        spring-beans-2.5.5.jar
        spring-context-2.5.5.jar
        spring-core-2.5.5.jar
        spring-jms-2.5.5.jar
        spring-tx-2.5.5.jar
        spring-web-2.5.5.jar
        spring-webmvc-2.5.5.jar
        xbean-spring-3.4.jar
        msbase.jar
        mssqlserver.jar
        msutil.jar
        mysql-connector-java-5.0.4-bin.jar
        mysql.jar

 

鍩烘湰閰嶇疆

涓嬭澆 Apache ActiveMQ 涓嶇煡閬撶殑googlean

瀹夎鏈嶅姟鐨勬枃浠跺湪

apache-activemq-5.5.0\bin\win32 鐩綍涓嬮潰Linux鐗堜篃涓鏍峰ぇ鍚屽皬寮?榪愯.sh鏂囦歡灝卞ソ錛屽鏋滃嚭鐜伴棶棰樿鎵撳紑榪愯鏂囦歡鏌ョ湅閲岄潰鍙傛暟

activemq.bat鐩存帴榪愯鍚姩鏈嶅姟

InstallService.bat瀹夎鎴恮indos鏈嶅姟

UninstallService.bat鍗歌澆windos鏈嶅姟

瀹夎鎴愬姛鍚?/p>

鍦ㄨ繍琛屼腑 cmd

netstat -an|find “61616” 鏌ヨ绔彛鏄惁宸茬粡鎵撳紑 

D:\apache-activemq-5.5.0\conf\activemq.xml 鏈枃浠朵負鏈嶅姟閰嶇疆鏂囦歡

     <transportConnectors>
            <transportConnector name="openwire" uri="tcp://0.0.0.0:61618"/>
        </transportConnectors>

榪欎竴欏逛負绔彛璁劇疆

榛樿涓?“61616”

鐩墠浠indos涓轟緥

鎶妔pring鍜岄」鐩仈緋昏搗鏉?/p>

絎竴姝ヤ負web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<!-- 閰嶇疆Spring-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/classes/NewFileMQ.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>


  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
    
</web-app>

鍏堣 涓涓嬫秷鎭槦鍒楋紙涔熷氨鏄竴瀵逛竴錛?/p>

NewFileMQ.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://activemq.apache.org/schema/core 
  http://activemq.apache.org/schema/core/activemq-core.xsd">

  <!-- 閰嶇疆JMS閾炬帴妯$増 -->  
            <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">  
                    <property name="brokerURL" value="tcp://localhost:61618"/>  
            </bean>  
      
            <!-- 閰嶇疆JMS妯$増 -->  
            <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
                    <property name="connectionFactory" ref="connectionFactory"/>  
        <property name="pubSubDomain" value="false" />  
        <property name="explicitQosEnabled" value="true" /> <!-- deliveryMode, priority, timeToLive 鐨勫紑鍏籌紝瑕佺敓鏁堬紝蹇呴』閰嶇疆涓簍rue錛岄粯璁alse-->  
        <property name="deliveryMode" value="1" /> <!-- 鍙戦佹ā寮?nbsp; DeliveryMode.NON_PERSISTENT=1:闈炴寔涔?; DeliveryMode.PERSISTENT=2:鎸佷箙-->  
                    
            </bean>  
      

            <!-- 鍙戦佹秷鎭殑鐩殑鍦幫紙涓涓槦鍒楋級 -->
            <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">  
                    <!-- 璁劇疆娑堟伅闃熷垪鐨勫悕瀛?-->
                    <constructor-arg index="0" value="HelloJmsQueue"/>  
            </bean>    

    <bean id="sender" class="message.Sender">
        <property name="jmsTemplate" ref="jmsTemplate"></property>
    </bean>
<!--娑堟伅鑾峰彇綾?->
    <bean id="receive" class="message.Receiver"></bean>
    <!--緇欐秷鎭幏鍙栫被鍔犱釜鐩戝惉璁╀粬鑳借嚜鍔ㄨ幏鍙栨秷鎭?->
    <bean id="listenerContainer"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"></property>
        <property name="destination" ref="destination"></property>
        <property name="messageListener" ref="receive"></property>
    </bean>
</beans>

鍙戦佹秷鎭殑綾?/p>

package message;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;


public class Sender {

    private JmsTemplate jmsTemplate;

    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }
    
    public void send(final String text){
        System.out.println("---Send:"+text);
        jmsTemplate.send(new MessageCreator(){

            public Message createMessage(Session arg0) throws JMSException {
                // TODO Auto-generated method stub
                return arg0.createTextMessage(text);
            }
            
        });
    }
    
    public static void main(String[] args) {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");  
        JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");  
        Destination destination = (Destination) ctx.getBean("destination");  
for(int i=0;i<10;i++){
    final String dd = i+"";
//jmsTemplate.convertAndSend(destination,"鍙戦佹秷鎭細 ActiveMQ Text Message錛?+dd);
        template.send(destination, new MessageCreator() {  
                public Message createMessage(Session session) throws JMSException {  
                        return session.createTextMessage("鍙戦佹秷鎭細 ActiveMQ Text Message錛?+dd);  
                }  
        });  
}
        System.out.println("鎴愬姛鍙戦佷簡涓鏉MS娑堟伅");  
}  
}
鎺ユ敹娑堟伅綾?/p>

package message;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.jms.core.JmsTemplate;


public class Receiver implements MessageListener {
//鎺ュ惉鎺ユ敹
    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            TextMessage text = (TextMessage) message;

            try {
                System.out.println("Receive:絎洓涓帴鏀惰? + text.getText());
            } catch (JMSException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
    //鎵嬪姩鎺ユ敹
       public static void main(String[] args) throws JMSException {  
           ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");  
           JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");  
           Destination destination = (Destination) ctx.getBean("destination");  
           while (true) {  
                   TextMessage txtmsg = (TextMessage) template.receive(destination);  
                   if (null != txtmsg)  
                           System.out.println("鏀跺埌娑堟伅鍐呭涓? " + txtmsg.getText());  
                   else  
                           break;  
           }  
   }  
}

鍙戝竷鑰呮ā寮?/p>

鍏跺疄涓ょ妯″紡鍏跺畠璁劇疆鍩烘湰鐩稿悓

鍞竴涓嶇敤鐨勫氨鏄?Spring閰嶇疆鏂囦歡涓妸涓涓嬭繖孌墊浛鎹竴涓嬪氨鍙互浜?/p>

            <!-- 鍙戦佹秷鎭殑鐩殑鍦幫紙涓涓槦鍒楋級 -->
            <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">  
                    <!-- 璁劇疆娑堟伅闃熷垪鐨勫悕瀛?-->
                    <constructor-arg index="0" value="HelloJmsTopic"/>  
            </bean>    

 








http://hi.baidu.com/wu_chao/blog/item/1d9aa2772e663107b051b9cc.html 



abin 2012-08-26 20:18 鍙戣〃璇勮
]]>
spring-activemq http://m.tkk7.com/stevenjohn/archive/2012/08/25/386274.htmlabinabinSat, 25 Aug 2012 13:34:00 GMThttp://m.tkk7.com/stevenjohn/archive/2012/08/25/386274.htmlhttp://m.tkk7.com/stevenjohn/comments/386274.htmlhttp://m.tkk7.com/stevenjohn/archive/2012/08/25/386274.html#Feedback0http://m.tkk7.com/stevenjohn/comments/commentRss/386274.htmlhttp://m.tkk7.com/stevenjohn/services/trackbacks/386274.html

1,欏圭洰涓墍寮曠殑鍖?/span>

 

2錛屽彂閫佸拰鎺ュ彈娑堟伅鐨勪唬鐮侊紝姣忎釜閮芥槸涓涓狫unit鍑芥暟

package test;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import junit.framework.TestCase;

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class ActivemqTest extends TestCase {
 @Test
 public void testJmsTemplateSend() {      

  ApplicationContext ctx = new FileSystemXmlApplicationContext(
    "WebRoot/WEB-INF/applicationContext.xml");
  JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
  Destination destination = (Destination) ctx.getBean("destination");

  template.send(destination, new MessageCreator() {
   public Message createMessage(Session session) throws JMSException {
    return session.createTextMessage("鍙戦佹秷鎭細Hello ActiveMQ Text Message錛?);
   }
  });
  System.out.println("鎴愬姛鍙戦佷簡涓鏉MS娑堟伅");
 }

 @Test
 public void testJmsTemplateReceive() throws JMSException {
  ApplicationContext ctx = new FileSystemXmlApplicationContext(
    "WebRoot/WEB-INF/applicationContext.xml");
  JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
  Destination destination = (Destination) ctx.getBean("destination");
  while (true) {
   TextMessage txtmsg = (TextMessage) template.receive(destination);
   if (null != txtmsg)
    System.out.println("鏀跺埌娑堟伅鍐呭涓? " + txtmsg.getText());
   else
    break;
  }
 }

 public static void main(String[] args) throws Exception{
  new ActivemqTest().testJmsTemplateSend();
  
  new ActivemqTest().testJmsTemplateReceive();
 }
}

 

3錛宎pplicationContext.xml鏂囦歡鍐呭

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
       xmlns:context="http://www.springframework.org/schema/context
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
 
    <!-- 閰嶇疆JMS榪炴帴宸ュ巶 -->  
    <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">  
        <property name="brokerURL" value="tcp://localhost:61616"/>  
    </bean>  
 
    <!-- 閰嶇疆JMS妯$増 -->  
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
        <property name="connectionFactory" ref="connectionFactory"/>  
    </bean>  
 
    <!-- 鍙戦佹秷鎭殑鐩殑鍦幫紙涓涓槦鍒楋級 -->  
    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">  
        <!-- Set the Queue Name -->  
        <constructor-arg index="0" value="HelloWorldQueue"/>  
    </bean>  
</beans>




http://blog.csdn.net/hnzhangshilong/article/details/6629780



abin 2012-08-25 21:34 鍙戣〃璇勮
]]>
ActiveMQ StreamMessage http://m.tkk7.com/stevenjohn/archive/2012/08/02/384622.htmlabinabinThu, 02 Aug 2012 08:35:00 GMThttp://m.tkk7.com/stevenjohn/archive/2012/08/02/384622.htmlhttp://m.tkk7.com/stevenjohn/comments/384622.htmlhttp://m.tkk7.com/stevenjohn/archive/2012/08/02/384622.html#Feedback0http://m.tkk7.com/stevenjohn/comments/commentRss/384622.htmlhttp://m.tkk7.com/stevenjohn/services/trackbacks/384622.htmlpackage org.abin.lee.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class MapSender {

 private static final int SEND_NUMBER = 5;

 public static void main(String[] args) {
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
    ActiveMQConnection.DEFAULT_USER,
    ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
  Connection connection = null;
  Session session;
  Destination destination = null;
  MessageProducer messageProducer;
  try {
   connection=connectionFactory.createConnection();
   connection.start();
   session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
   destination=session.createQueue("FirstQueue");
   messageProducer=session.createProducer(destination);
   messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
   
   MapMessage map=session.createMapMessage();
   map.setBoolean("flag", true);
   map.setDouble("dou", 1.01);
   map.setInt("zx", 88);
   map.setString("zifu", "zzzzzz");
   messageProducer.send(map);
   session.commit();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }


}






package org.abin.lee.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MapMessage;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class MapReceiver {public static void main(String[] args) {
 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
   ActiveMQConnection.DEFAULT_USER,
   ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
 Connection connection = null;
 Session session;
 Destination destination;
 MessageConsumer consumer;
 try {
  connection = connectionFactory.createConnection();
  connection.start();
  session = connection.createSession(Boolean.FALSE,
    Session.AUTO_ACKNOWLEDGE);
  destination = session.createQueue("FirstQueue");
  consumer = session.createConsumer(destination);
  while(true){
   MapMessage map=(MapMessage)consumer.receive(500000);
   if (null != map) {
    boolean flag=map.getBoolean("falg");
    System.out.println("AcitveMQ 鎺ユ敹鍒扮殑娑堟伅  flag="+flag);
    double dou=map.getDouble("dou");
    System.out.println("AcitveMQ 鎺ユ敹鍒扮殑娑堟伅  dou="+dou);
    int zx=map.getInt("zx");
    System.out.println("AcitveMQ 鎺ユ敹鍒扮殑娑堟伅  zx="+zx);
    String zifu=map.getString("zifu");
    System.out.println("AcitveMQ 鎺ユ敹鍒扮殑娑堟伅  zifu="+zifu);
   }else
    break;

  }
  
   

 } catch (Exception e) {
  e.printStackTrace();
 }

}
}



abin 2012-08-02 16:35 鍙戣〃璇勮
]]>
ActiveMQ鍏ラ棬紺轟緥 http://m.tkk7.com/stevenjohn/archive/2012/08/02/384609.htmlabinabinThu, 02 Aug 2012 06:59:00 GMThttp://m.tkk7.com/stevenjohn/archive/2012/08/02/384609.htmlhttp://m.tkk7.com/stevenjohn/comments/384609.htmlhttp://m.tkk7.com/stevenjohn/archive/2012/08/02/384609.html#Feedback0http://m.tkk7.com/stevenjohn/comments/commentRss/384609.htmlhttp://m.tkk7.com/stevenjohn/services/trackbacks/384609.html浼佷笟涓悇欏圭洰涓浉浜掑崗浣滅殑鏃跺欏彲鑳界敤寰楀埌娑堟伅閫氱煡鏈哄埗銆傛瘮濡傛湁涓滆タ鏇存柊浜嗭紝鍙互閫氱煡鍋氱儲寮曘?/p>

鍦?Java 閲屾湁 JMS 鐨勫涓疄鐜般傚叾涓?apache 涓嬬殑 ActiveMQ 灝辨槸涓嶉敊鐨勯夋嫨銆侫ctiveMQ 鏄疉pache鍑哄搧錛屾渶嫻佽鐨勶紝鑳藉姏寮哄姴鐨勫紑婧愭秷鎭葷嚎銆侫ctiveMQ 鏄竴涓畬鍏ㄦ敮鎸丣MS1.1鍜孞2EE 1.4瑙勮寖鐨?JMS Provider瀹炵幇銆傝繖閲岀ず渚嬩笅浣跨敤 ActiveMQ

鐢?ActiveMQ 鏈濂借繕鏄簡瑙d笅 JMS

JMS 鍏叡 鐐瑰鐐瑰煙 鍙戝竷/璁㈤槄鍩?/strong>
ConnectionFactory QueueConnectionFactory TopicConnectionFactory
Connection QueueConnection TopicConnection
Destination Queue Topic
Session QueueSession TopicSession
MessageProducer QueueSender TopicPublisher
MessageConsumer QueueReceiver TopicSubscriber

JMS 瀹氫箟浜嗕袱縐嶆柟寮忥細Quere錛堢偣瀵圭偣錛夛紱Topic錛堝彂甯?璁㈤槄錛夈?/p>

ConnectionFactory 鏄繛鎺ュ伐鍘傦紝璐熻矗鍒涘緩Connection銆?/p>

Connection 璐熻矗鍒涘緩 Session銆?/p>

Session 鍒涘緩 MessageProducer錛堢敤鏉ュ彂娑堟伅錛?鍜?MessageConsumer錛堢敤鏉ユ帴鏀舵秷鎭級銆?/p>

Destination 鏄秷鎭殑鐩殑鍦般?/p>

璇︾粏鐨勫彲浠ョ綉涓婃壘浜?JMS 瑙勮寖錛堟湁涓枃鐗堬級銆?/p>

涓嬭澆 apache-activemq-5.3.0銆?a target="_blank">http://activemq.apache.org/download.html 錛岃В鍘嬶紝鐒跺悗鍙屽嚮 bin/activemq.bat銆傝繍琛屽悗錛屽彲浠ュ湪 http://localhost:8161/admin 瑙傚療銆備篃鏈?demo錛?http://localhost:8161/demo 銆傛妸 activemq-all-5.3.0.jar 鍔犲叆 classpath銆?/p>

Jms 鍙戦?浠g爜錛?/p>public static void main(String[] args) throws Exception {   
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();   
  
    Connection connection = connectionFactory.createConnection();   
    connection.start();   
  
    Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);   
    Destination destination = session.createQueue("my-queue");   
  
    MessageProducer producer = session.createProducer(destination);   
    for(int i=0; i<3; i++) {   
        MapMessage message = session.createMapMessage();   
        message.setLong("count", new Date().getTime());   
        Thread.sleep(1000);   
        //閫氳繃娑堟伅鐢熶駭鑰呭彂鍑烘秷鎭?nbsp;  
        producer.send(message);   
    }   
    session.commit();   
    session.close();   
    connection.close();   
}



Jms 鎺ユ敹浠g爜錛?/p>
public static void main(String[] args) throws Exception {   
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();   
  
    Connection connection = connectionFactory.createConnection();   
    connection.start();   
  
    final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);   
    Destination destination = session.createQueue("my-queue");   
  
    MessageConsumer consumer = session.createConsumer(destination);   
    int i=0;   
    while(i<3) {   
        i++;   
        MapMessage message = (MapMessage) consumer.receive();   
        session.commit();   
  
        //TODO something....   
        System.out.println("鏀跺埌娑堟伅錛? + new Date(message.getLong("count")));   
    }   
  
    session.close();   
    connection.close();   
}



JMS浜旂娑堟伅鐨勫彂閫?鎺ユ敹鐨勪緥瀛?/font>

杞嚜錛?/font>http://chenjumin.javaeye.com/blog/687124  

1銆佹秷鎭彂閫?/p>//榪炴帴宸ュ巶  
ConnectionFactory connFactory = new ActiveMQConnectionFactory(  
        ActiveMQConnection.DEFAULT_USER,  
        ActiveMQConnection.DEFAULT_PASSWORD,  
        "tcp://localhost:61616");  
 
//榪炴帴鍒癑MS鎻愪緵鑰?nbsp; 
Connection conn = connFactory.createConnection();  
conn.start();  
 
//浜嬪姟鎬т細璇濓紝鑷姩紜娑堟伅  
Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);  
 
//娑堟伅鐨勭洰鐨勫湴  
Destination destination = session.createQueue("queue.hello");  
 
//娑堟伅鐢熶駭鑰?nbsp; 
MessageProducer producer = session.createProducer(destination);  
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); //涓嶆寔涔呭寲  
 
 
//鏂囨湰娑堟伅  
TextMessage textMessage = session.createTextMessage("鏂囨湰娑堟伅");  
producer.send(textMessage);  
 
//閿煎娑堟伅  
MapMessage mapMessage = session.createMapMessage();  
mapMessage.setLong("age", new Long(32));  
mapMessage.setDouble("sarray", new Double(5867.15));  
mapMessage.setString("username", "閿煎娑堟伅");  
producer.send(mapMessage);  
 
//嫻佹秷鎭?nbsp; 
StreamMessage streamMessage = session.createStreamMessage();  
streamMessage.writeString("streamMessage嫻佹秷鎭?);  
streamMessage.writeLong(55);  
producer.send(streamMessage);  
 
//瀛楄妭娑堟伅  
String s = "BytesMessage瀛楄妭娑堟伅";  
BytesMessage bytesMessage = session.createBytesMessage();  
bytesMessage.writeBytes(s.getBytes());  
producer.send(bytesMessage);  
 
//瀵硅薄娑堟伅  
User user = new User("cjm", "瀵硅薄娑堟伅"); //User瀵硅薄蹇呴』瀹炵幇Serializable鎺ュ彛  
ObjectMessage objectMessage = session.createObjectMessage();  
objectMessage.setObject(user);  
producer.send(objectMessage);  
 
 
session.commit(); //鍦ㄤ簨鍔℃т細璇濅腑錛屽彧鏈塩ommit涔嬪悗錛屾秷鎭墠浼氱湡姝e埌杈劇洰鐨勫湴  
producer.close();  
session.close();  
conn.close(); 



2銆佹秷鎭帴鏀訛細閫氳繃娑堟伅鐩戝惉鍣ㄧ殑鏂瑰紡鎺ユ敹娑堟伅


public class Receiver implements MessageListener{  
    private boolean stop = false;  
      
    public void execute() throws Exception {  
        //榪炴帴宸ュ巶  
        ConnectionFactory connFactory = new ActiveMQConnectionFactory(  
                ActiveMQConnection.DEFAULT_USER,  
                ActiveMQConnection.DEFAULT_PASSWORD,  
                "tcp://localhost:61616");  
          
        //榪炴帴鍒癑MS鎻愪緵鑰?nbsp; 
        Connection conn = connFactory.createConnection();  
        conn.start();  
          
        //浜嬪姟鎬т細璇濓紝鑷姩紜娑堟伅  
        Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);  
          
        //娑堟伅鐨勬潵婧愬湴  
        Destination destination = session.createQueue("queue.hello");  
          
        //娑堟伅娑堣垂鑰?nbsp; 
        MessageConsumer consumer = session.createConsumer(destination);  
        consumer.setMessageListener(this);  
          
        //絳夊緟鎺ユ敹娑堟伅  
        while(!stop){  
            Thread.sleep(5000);  
        }  
          
        session.commit();  
          
        consumer.close();  
        session.close();  
        conn.close();  
    }  
 
    public void onMessage(Message m) {  
        try{  
            if(m instanceof TextMessage){ //鎺ユ敹鏂囨湰娑堟伅  
                TextMessage message = (TextMessage)m;  
                System.out.println(message.getText());  
            }else if(m instanceof MapMessage){ //鎺ユ敹閿煎娑堟伅  
                MapMessage message = (MapMessage)m;  
                System.out.println(message.getLong("age"));  
                System.out.println(message.getDouble("sarray"));  
                System.out.println(message.getString("username"));  
            }else if(m instanceof StreamMessage){ //鎺ユ敹嫻佹秷鎭?nbsp; 
                StreamMessage message = (StreamMessage)m;  
                System.out.println(message.readString());  
                System.out.println(message.readLong());  
            }else if(m instanceof BytesMessage){ //鎺ユ敹瀛楄妭娑堟伅  
                byte[] b = new byte[1024];  
                int len = -1;  
                BytesMessage message = (BytesMessage)m;  
                while((len=message.readBytes(b))!=-1){  
                    System.out.println(new String(b, 0, len));  
                }  
            }else if(m instanceof ObjectMessage){ //鎺ユ敹瀵硅薄娑堟伅  
                ObjectMessage message = (ObjectMessage)m;  
                User user = (User)message.getObject();  
                System.out.println(user.getUsername() + " _ " + user.getPassword());  
            }else{  
                System.out.println(m);  
            }  
              
            stop = true;  
        }catch(JMSException e){  
            stop = true;  
            e.printStackTrace();  
        }  
    }  





http://blog.csdn.net/caihaijiang/article/details/5903296


abin 2012-08-02 14:59 鍙戣〃璇勮
]]>
ActiveMQ鍏ラ棬瀹炰緥http://m.tkk7.com/stevenjohn/archive/2012/07/07/382440.htmlabinabinSat, 07 Jul 2012 06:47:00 GMThttp://m.tkk7.com/stevenjohn/archive/2012/07/07/382440.htmlhttp://m.tkk7.com/stevenjohn/comments/382440.htmlhttp://m.tkk7.com/stevenjohn/archive/2012/07/07/382440.html#Feedback0http://m.tkk7.com/stevenjohn/comments/commentRss/382440.htmlhttp://m.tkk7.com/stevenjohn/services/trackbacks/382440.html

1.涓嬭澆ActiveMQ

鍘誨畼鏂圭綉绔欎笅杞斤細http://activemq.apache.org/

2.榪愯ActiveMQ

瑙e帇緙゛pache-activemq-5.5.1-bin.zip錛岀劧鍚庡弻鍑籥pache-activemq-5.5.1\bin\activemq.bat榪愯ActiveMQ紼嬪簭銆?/p>

鍚姩ActiveMQ浠ュ悗錛岀櫥闄嗭細http://localhost:8161/admin/錛屽垱寤轟竴涓猀ueue錛屽懡鍚嶄負FirstQueue銆?/p>

3.鍒涘緩Eclipse欏圭洰騫惰繍琛?/h2>

鍒涘緩project錛欰ctiveMQ-5.5錛屽茍瀵煎叆apache-activemq-5.5.1\lib鐩綍涓嬮渶瑕佺敤鍒扮殑jar鏂囦歡錛岄」鐩粨鏋勫涓嬪浘鎵紺猴細

3.1.Sender.java

package com.xuwei.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Sender {
    private static final int SEND_NUMBER = 5;

    public static void main(String[] args) {
        // ConnectionFactory 錛氳繛鎺ュ伐鍘傦紝JMS 鐢ㄥ畠鍒涘緩榪炴帴
        ConnectionFactory connectionFactory;
        // Connection 錛欽MS 瀹㈡埛绔埌JMS Provider 鐨勮繛鎺?br />        Connection connection = null;
        // Session錛?涓涓彂閫佹垨鎺ユ敹娑堟伅鐨勭嚎紼?br />        Session session;
        // Destination 錛氭秷鎭殑鐩殑鍦?娑堟伅鍙戦佺粰璋?
        Destination destination;
        // MessageProducer錛氭秷鎭彂閫佽?br />        MessageProducer producer;
        // TextMessage message;
        // 鏋勯燙onnectionFactory瀹炰緥瀵硅薄錛屾澶勯噰鐢ˋctiveMq鐨勫疄鐜癹ar
        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "tcp://localhost:61616");
        try {
            // 鏋勯犱粠宸ュ巶寰楀埌榪炴帴瀵硅薄
            connection = connectionFactory.createConnection();
            // 鍚姩
            connection.start();
            // 鑾峰彇鎿嶄綔榪炴帴
            session = connection.createSession(Boolean.TRUE,
                    Session.AUTO_ACKNOWLEDGE);
            // 鑾峰彇session娉ㄦ剰鍙傛暟鍊紉ingbo.xu-queue鏄竴涓湇鍔″櫒鐨剄ueue錛岄』鍦ㄥ湪ActiveMq鐨刢onsole閰嶇疆
            destination = session.createQueue("FirstQueue");
            // 寰楀埌娑堟伅鐢熸垚鑰呫愬彂閫佽呫?br />            producer = session.createProducer(destination);
            // 璁劇疆涓嶆寔涔呭寲錛屾澶勫涔狅紝瀹為檯鏍規嵁欏圭洰鍐沖畾
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            // 鏋勯犳秷鎭紝姝ゅ鍐欐錛岄」鐩氨鏄弬鏁幫紝鎴栬呮柟娉曡幏鍙?br />            sendMessage(session, producer);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection)
                    connection.close();
            } catch (Throwable ignore) {
            }
        }
    }

    public static void sendMessage(Session session, MessageProducer producer)
            throws Exception {
        for (int i = 1; i <= SEND_NUMBER; i++) {
            TextMessage message = session
                    .createTextMessage("ActiveMq 鍙戦佺殑娑堟伅" + i);
            // 鍙戦佹秷鎭埌鐩殑鍦版柟
            System.out.println("鍙戦佹秷鎭細" + "ActiveMq 鍙戦佺殑娑堟伅" + i);
            producer.send(message);
        }
    }
}

 

 

 

 

3.2.Receiver.java

package com.xuwei.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Receiver {
    public static void main(String[] args) {
        // ConnectionFactory 錛氳繛鎺ュ伐鍘傦紝JMS 鐢ㄥ畠鍒涘緩榪炴帴
        ConnectionFactory connectionFactory;
        // Connection 錛欽MS 瀹㈡埛绔埌JMS Provider 鐨勮繛鎺?br />        Connection connection = null;
        // Session錛?涓涓彂閫佹垨鎺ユ敹娑堟伅鐨勭嚎紼?br />        Session session;
        // Destination 錛氭秷鎭殑鐩殑鍦?娑堟伅鍙戦佺粰璋?
        Destination destination;
        // 娑堣垂鑰咃紝娑堟伅鎺ユ敹鑰?br />        MessageConsumer consumer;
        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "tcp://localhost:61616");
        try {
            // 鏋勯犱粠宸ュ巶寰楀埌榪炴帴瀵硅薄
            connection = connectionFactory.createConnection();
            // 鍚姩
            connection.start();
            // 鑾峰彇鎿嶄綔榪炴帴
            session = connection.createSession(Boolean.FALSE,
                    Session.AUTO_ACKNOWLEDGE);
            // 鑾峰彇session娉ㄦ剰鍙傛暟鍊紉ingbo.xu-queue鏄竴涓湇鍔″櫒鐨剄ueue錛岄』鍦ㄥ湪ActiveMq鐨刢onsole閰嶇疆
            destination = session.createQueue("FirstQueue");
            consumer = session.createConsumer(destination);
            while (true) {
                //璁劇疆鎺ユ敹鑰呮帴鏀舵秷鎭殑鏃墮棿錛屼負浜嗕究浜庢祴璇曪紝榪欓噷璋佸畾涓?00s
                TextMessage message = (TextMessage) consumer.receive(100000);
                if (null != message) {
                    System.out.println("鏀跺埌娑堟伅" + message.getText());
                } else {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection)
                    connection.close();
            } catch (Throwable ignore) {
            }
        }
    }
}

 

 

 

4.娉ㄦ剰浜嬮」

  1. 鏈鍚庢帴鏀惰呰窡鍙戦佽呭湪涓嶅悓鐨勬満鍣ㄤ笂嫻嬭瘯
  2. 欏圭洰鎵寮曠敤鐨刯ar鏈鍚庡湪ActiveMQ涓嬬殑lib涓壘錛岃繖鏍蜂笉浼氬嚭鐜扮増鏈啿紿併?

5.嫻嬭瘯榪囩▼

鍥犱負鏄湪鍗曟満涓婃祴璇曪紝鎵浠ラ渶瑕佸紑鍚袱涓猠clipse錛屾瘡涓涓猠clipse閮芥湁鑷韓鐨剋orkspace銆傛垜浠湪eclipse1涓繍琛孯eceiver錛屽湪eclipse2涓繍琛孲ender銆?/p>

鍒氬紑濮媏clipse1涓繍琛孯eceiver浠ュ悗console浠嬮潰娌℃湁浠諱綍淇℃伅錛屽湪eclipse2涓繍琛孲ender浠ュ悗錛宔clipse2涓殑console鏄劇ず濡備笅淇℃伅錛?/p>

鍙戦佹秷鎭細ActiveMq 鍙戦佺殑娑堟伅1
鍙戦佹秷鎭細ActiveMq 鍙戦佺殑娑堟伅2
鍙戦佹秷鎭細ActiveMq 鍙戦佺殑娑堟伅3
鍙戦佹秷鎭細ActiveMq 鍙戦佺殑娑堟伅4
鍙戦佹秷鎭細ActiveMq 鍙戦佺殑娑堟伅5

鑰屽洖鍒癳clipse1涓彂鐜癱onsole鐣岄潰鍑虹幇濡備笅淇℃伅錛?/p>

鏀跺埌娑堟伅ActiveMq 鍙戦佺殑娑堟伅1
鏀跺埌娑堟伅ActiveMq 鍙戦佺殑娑堟伅2
鏀跺埌娑堟伅ActiveMq 鍙戦佺殑娑堟伅3
鏀跺埌娑堟伅ActiveMq 鍙戦佺殑娑堟伅4
鏀跺埌娑堟伅ActiveMq 鍙戦佺殑娑堟伅5

 PS:2012-2-27

浠婂ぉ鍙戠幇嫻嬭瘯騫朵笉闇瑕佸紑鍚袱涓猠clipse錛屽湪涓涓猠clipse涓嬮〉鍙互鍚姩澶氫釜紼嬪簭錛屽茍涓旀湁澶氫釜console錛屽湪涓婇潰鐨凴eceiver.java涓紝璁劇疆涓涓緝澶х殑鏃墮棿錛屾瘮濡俽eceive(500000)錛屽涓嬩唬鐮佹墍紺猴細

TextMessage message = (TextMessage) consumer.receive(500000);

榪欎釜鏃跺欒繍琛孯eceiver.java鐨勮瘽錛屼細浣垮緱榪欎釜Receiver.java涓鐩磋繍琛?00縐掞紝鍦╡clipse涓彲浠ュ彂鐜幫細

鐐瑰嚮閭d釜綰㈣壊鏂瑰潡鍙互鎵嬪姩鍋滄榪愯紼嬪簭銆?/p>

榪愯鐜﹔eceiver浠ュ悗鎴戜滑鍦ㄨ繍琛宻ender錛屽湪榪愯瀹宻ender浠ュ悗錛屾垜浠鍒囨崲鍒皉eceiver鐨刢onsole錛屽涓嬪浘鎵紺猴細





http://www.cnblogs.com/xwdreamer/archive/2012/02/21/2360818.html


 



abin 2012-07-07 14:47 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 日本特黄a级高清免费大片| 999久久久免费精品播放| 黄a大片av永久免费| 亚洲sss综合天堂久久久| 亚洲综合免费视频| 91亚洲精品麻豆| 在线视频免费观看高清| ASS亚洲熟妇毛茸茸PICS| 免费精品国产自产拍在| 国产成人亚洲综合一区| 免费无码又爽又刺激高潮的视频| 亚洲六月丁香婷婷综合| 永久久久免费浮力影院| 视频免费1区二区三区| 中文字幕亚洲综合久久菠萝蜜 | 亚洲精品人成网线在线播放va| 天天干在线免费视频| 亚洲a∨国产av综合av下载| 国产乱子影视频上线免费观看| 国产精品自拍亚洲| 亚洲午夜国产精品无码老牛影视| 国产成人免费ā片在线观看老同学| 亚洲av中文无码乱人伦在线r▽| 91av在线免费视频| 亚洲性无码AV中文字幕| 免费久久精品国产片香蕉| WWW免费视频在线观看播放 | 亚洲国产综合专区在线电影| 最近中文字幕免费完整| 亚洲欧美熟妇综合久久久久| 亚洲成a人片在线观看老师| 你懂得的在线观看免费视频| 久久亚洲精品无码aⅴ大香| 成人午夜性A级毛片免费| 亚洲免费日韩无码系列| 亚洲精品福利视频| 色www永久免费视频| 中文字幕高清免费不卡视频| 亚洲国产人成在线观看| 亚洲国产日韩成人综合天堂| 久久国产一片免费观看|