1. 程式人生 > >《Pro Spring》學習筆記之Spring+ActiveMQ實現Queue通訊(點對點)

《Pro Spring》學習筆記之Spring+ActiveMQ實現Queue通訊(點對點)

spring配置檔案:

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns
="http://www.springframework.org/schema/beans"
    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">

<bean id
="connectionFactory"class="org.apache.activemq.ActiveMQConnectionFactory">
  
<property name="brokerURL">
    
<value>tcp://gx:61616</value>
</property>
 
</bean>

<bean id="jmsTemplate"class="org.springframework.jms.core.JmsTemplate">
   
<property name="connectionFactory"
>
     
<ref bean="connectionFactory"/>
   
</property>

  
   
</bean>

<bean id="destination"class="org.apache.activemq.command.ActiveMQQueue">
  
<constructor-arg index="0">
    
<value>HelloWorldQueue</value>
  
</constructor-arg>
</bean>
</beans>

 生產者:

package
 ch13.JMS;

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;

publicclass HelloWorldSender  {
    
publicstaticvoid main(String args[]) throws Exception{
        ApplicationContext context 
=new ClassPathXmlApplicationContext(
                
new String[] {"ch13/JMS/applicationContext.xml"});
        JmsTemplate jmsTemplate 
= (JmsTemplate) context.getBean("jmsTemplate");
        Destination destination 
= (Destination) context.getBean("destination");
        jmsTemplate.send(destination, 
new MessageCreator() {
            
public Message createMessage(Session session) throws JMSException {
                
return session.createTextMessage("11");
            }

        }
);
    }

    


}

消費者:

package ch13.JMS;

import javax.jms.Destination;
import javax.jms.TextMessage;

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


publicclass HelloWorldReciver  {

    
      
publicstaticvoid main(String args[]) throws Exception{
            ApplicationContext context
=new ClassPathXmlApplicationContext(
                    
new String[]{"ch13/JMS/applicationContext.xml"});
            JmsTemplate jmsTemplate
=(JmsTemplate)context.getBean("jmsTemplate");
            Destination destination
=(Destination)context.getBean("destination");
            System.out.println(
"will wait:"+jmsTemplate.getReceiveTimeout()+" seconds for message");
            TextMessage msg
=(TextMessage)jmsTemplate.receive(destination);
            System.out.println(
"reviced msg is:"+msg.getText());
      }

    
   
}

測試步驟:

1、啟動ActiveMQ  (bin下的activemq.bat)

2、執行HelloWorldReciver
       出現以下提示:will wait:-1 seconds for message

3、執行HelloWorldSender
       出現以下結果:

        will wait:-1 seconds for message
        reviced msg is:11

幾點說明:JmsTemplate的預設reserviceTimeout為-1,也就是用不超時,等待接受訊息,單位為毫秒

相關推薦

Pro Spring學習筆記Spring+ActiveMQ實現Queue通訊

spring配置檔案: <?xml version="1.0" encoding="UTF-8"?><beans    xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="h

Spring學習筆記Spring MVC的高階技術

本篇主要介紹利用Spring MVC處理檔案上傳,異常處理,為控制器新增通知以及跨重定向請求傳遞資料。 1.Spring MVC處理檔案上傳 1.1使用multipart格式提交表單 在有檔案上傳的表單中,我們需要使用multipart格式的資料來

mybatis學習筆記——mybatis的XML配置檔案全域性配置檔案

MyBatis的配置檔案包含了會深深影響MyBatis行為的設定(settings)和屬性(properties)資訊。我們詳細瞭解一下MyBatis的各種標籤的作用以及使用方法。 properties properties:配置,mybatis可以使用properties標籤來引入外部pr

學習筆記Openlayers3】要素繪製篇第三篇

直接以專案例項來進行講解要素繪製 需求(假如): 1.實現在地圖上畫點線面功能 2.自定義其樣式 3.支援編輯功能 需要用到的openlayers3中的ol.interaction.Draw 類。這是openlayers3提供的內建互動方式,除了這

學習筆記Openlayers3】要素儲存篇第四篇

上一篇中已經講了要素的繪製功能,既然要素都繪製出來了,繪製完就應該儲存起來了吧,那麼怎麼儲存呢?這一篇就是講解怎麼儲存繪製好的要素的。 個人用到過兩種儲存要素的方法,一種是通過WFS直接儲存要素入庫,另一種是通過ajax的方法通過專案的伺服器端儲

學習筆記Openlayers3】查詢分析篇第五篇

select count(*) from "+sourceName+" where 1=1 "; String sql = "select gid as gid,fname as name,ST_AsText(geom) as geowkt from "+sourceName+" wher

Spring學習筆記BeanDefinition

spring bean definition 在Spring容器中,Bean的實例以BeanDefinition來表示的。一個BeanDefinition描述了一個Bean實例。本文出自 “十裏稻花香” 博客,請務必保留此出處http://5880861.blog.51cto.com/587086

Spring學習筆記BeanFactory

spring bean factory BeanFactory是一個頂級接口,下面看下它是幹什麽的。這裏說了,它是訪問Spring Bean容器的根接口,是Bean容器的基本視圖。它的一些子接口,比如ListableBeanFactory和ConfigurableBeanFactory都有特別的而

Spring學習筆記ApplicationContext

spring application context 對於一個Application提供配置的核心接口,在應用運行的時候它是只讀的。一個ApplicationContext提供以下能力:1、可以通過BeanFactory中的方法訪問應用組件2、可以加載文件資源3、可以給已經註冊的監聽器發送事件4、

Spring學習筆記啟動

spring 原理 啟動分析今天,以ClassPathXmlApplicationContext為例來看一下,Spring啟動的時候都做了什麽重點看refresh()方法refresh()方法是在AbstractApplicationContext類中定義的ClassPathXmlApplicationCo

Sprng Cloud學習筆記Spring Cloud簡介

Spring Cloud Spring Cloud是一系列框架的有序集合(Spring Cloud並不是一個專案,它是一套專案的組合)。它利用Spring Boot的開發便利性巧妙地簡化了分散式系統基礎設施的開發,如服務發現註冊、配置中心、訊息匯流排、負載均衡、斷路器、資料監控等,都可以

1.Spring學習筆記 ———— Bean的例項化

    什麼是Bean的例項化?     在面向物件的程式中,想要使用某個物件,就需要先例項化這個物件。Spring中,想要使用容器中的Bean,也需要例項化Bean。     其類似於當我們需要建立一個類物件而去new這個類一樣。通常來說,當我們需要用到一個Bean的時

2.Spring學習筆記 ————IoC(控制反轉)

控制反轉(IoC),是Spring裡一個專有的名詞,其意思就是說,物件的例項由Spring容器來進行建立而不是我們自己手動建立,當我們在Spring容器中設定好Bean屬性後,Spring容器就會自動建立其例項,我們只要去呼叫Spring的Bean就行。 接下來是例子:

Spring學習筆記自動化裝配Bean

在Spring中可以使用Java程式碼、XML和自動化裝配三種方式來裝配Bean。從便利性角度來說,最強大的還是Spring的自動化配置,如果Spring能夠進行自動化裝配的話,那何苦還要顯式的將這些Bean裝配在一起呢? Spring從兩個角度來實現自動化裝配: 元件

Spring學習筆記前置通知、後置通知

在學習之前先提一個概念。 AsprctJ:Java社群裡最完整最流行的AOP框架。在Spring2.0以上版本中,可以使用基於AspectJ註解或基於XML配置的AOP。 啟用AspectJ註解支援 要在 Spring 中宣告 AspectJ 切面,

Spring學習筆記Bean的作用域

在預設情況下,Spring的應用上下文中所有的bean都是單例的形式建立的。也就是說,不管給定的一個bean被注入到其它bean多少次,每次注入的都是同一個例項。 在大多數情況下,單例bean是非常理想的方案。初始化和垃圾回收物件例項所帶來的成本只留給一些小規模任務,在這些任

Spring學習筆記--SpEL

本文內容來源於尚矽谷視訊教程,僅作為本文學習筆記使用,請勿轉載! 1.什麼是SpEL? Spring表示式語言,簡稱SpEL,是一個支援執行時查詢和操作物件的強大的表示式語言。語法類似於EL,格式為#{...}。SpEL為Bean的屬性進行動態賦值提供了便利。 2.可以做什

新手學習筆記Spring編寫AOP半自動代理

invoke proc pro aspect 新手 接口 src info nbsp 1.導包: 2.目標類 package oyb.service; public interface UserService { public void add(

spring學習筆記(一) Spring概述

數據庫 spring容器 oot 基礎知識 spa 遠程 組合 主動 拓展 博主Spring學習筆記整理大部分內容來自Spring實戰(第四版)這本書. 強烈建議新手購入或者需要電子書的留言. 在學習Spring之前,我們要了解這麽幾個問題:什麽是Spring?Sprin

ES學習筆記-AvgAggregation的實現過程分析

我們需要檢視資料的統計量時,均值是最重要的特徵之一。 對於海量資料,這類簡單的聚合ES可以做到秒級別返回。聚合是ES的特色功能。 那麼ES是如何實現這一功能的呢? 我們知道,ES的資料儲存在各個節點中, 所以ES的實現AvgAggregation時基本思路就是先統計各個節點,然後彙總。 先了解ES是