ActiveMQ初體驗
首先介紹下MQ,MQ英文名MessageQueue,中文名也就是大家用的消息隊列,幹嘛用的呢,
說白了就是一個消息的接受和轉發的容器,可用于消息推送。
下面介紹主題,就是今天為大家介紹的ActiveMQ:
他是Apache出品的一個開源的消息隊列軟體,運作在JVM下,支援多種語言,如JAVA,C++,C#。
現在先為大家介紹下如何配置ActiveMQ的伺服器端:
1、當然是下載下傳軟體了
當然開源的也是支援下載下傳source的,需要自己編譯下,這裡不做過多介紹
2、解壓後,進入\apache-activemq-5.8.0\bin\win32啟動activemq.bat。
系統會自動執行啟動過程,當然一般安裝失敗的情況是沒有裝JVM環境
3、在ActiveMQ中,有兩個概念,一個是生産者(Producer),另一個是消費者(Consumer),
生産者就是我們常說的釋出者,而消費者,就是訂閱者,
這樣解釋可能更好了解一下吧,
如果知道釋出訂閱模式的話,不知道的話,字面意思也很好了解,釋出者就是釋出消息的,
而訂閱者通過訂閱事件,将消息接收到;
<a href="http://blog.sina.com.cn/s/blog_4b5bc0110100kb8d.html">http://blog.sina.com.cn/s/blog_4b5bc0110100kb8d.html</a>
一:jms介紹
jms說白了就是java message service,是J2EE規範的一部分,跟jdbc差不多,sun隻提供了接口,
由各個廠商(provider)來進行具體的實作,然後使用者使用他們的jar包進行開發使用即可。
另外在jms的API中,jms傳遞消息有兩種方式,一種是點對點的Queue,還有一個是釋出訂閱的Topic方式。差別在于:
對于Queue模式,一個釋出者釋出消息,下面的接收者按隊列順序接收,比如釋出了10個消息,兩個接收者A,B那就是A,B總共會收到10條消息,不重複。
對于Topic模式,一個釋出者釋出消息,有兩個接收者A,B來訂閱,那麼釋出了10條消息,A,B各收到10條消息。
二:ActiveMQ介紹
activeMQ是apache下的一個開源jms産品,具體參見 apache官方網站;
Apache ActiveMQ is fast, supports many Cross Language Clients and Protocols ,
comes with easy to use Enterprise Integration Patterns and many advanced features
while fully supporting JMS 1.1 and J2EE 1.4. Apache ActiveMQ is released under the Apache 2.0 License
三:開始實作代碼
1:使用activeMQ來完成jms的發送,必須要下載下傳activeMQ,然後再本機安裝,并且啟動activeMQ的服務才行。
在官網下載下傳完成之後,運作bin目錄下面的activemq.bat,将activeMQ成功啟動。
2:發送端,sender
<a href="http://www.blogjava.net/pzxsheng/archive/2013/03/15/396462.html">http://www.blogjava.net/pzxsheng/archive/2013/03/15/396462.html</a>
ActiveMQ使用筆記(一)ActiveMQ的安裝
安裝要求:
部署需要jdk1.5及以上,編譯需要jdk1.5(java5)及以上
Java的環境變量(JAVA_HOME)必須設定,即jdk安裝的目錄,比如c:\Program Files\jdk.1.6
解壓
運作bin檔案夾下的activemq.bat
驗證是否運作成功:
此時,ActiveMQ已經安裝完成了,接下來配置登入監視控制台的使用者名和密碼。
打開conf檔案夾下的jetty.xml,找到
<bean id="securityConstraint">
<property name="name" value="BASIC" />
<property name="roles" value="admin" />
<property name="authenticate" value="false" />
</bean>
把authenticate屬性的值改成true即可,重新開機activemq.bat,再登入監視控制台,就需要輸入密碼了,預設的使用者名和密碼是admin/admin。roles屬性指的是登入的使用者角色,這些登入的使用者在jetty-realm.properties配置。
修改web的通路端口,在jetty.xml找到一下配置,修改8161即可。
<property name="connectors">
<list>
<bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<property name="port" value="8161" />
</bean>
</list>
</property>
ActiveMQ的運作日志存放在data檔案夾下的activemq.log中。
Linux和Aix系統下的安裝:
解壓:tar zxvf activemq-x.x.x.tar.gz,進入bin檔案夾,運作:./activemq start &,也可以隻運作:./activemq console。
驗證方式和安全性配置和windows下的配置一樣
activeMQ密碼配置
<a href="http://blog.163.com/czg_e/blog/static/4610456120133109443755/">http://blog.163.com/czg_e/blog/static/4610456120133109443755/</a>
<a href="http://blog.csdn.net/stevenprime/article/details/7091224">http://blog.csdn.net/stevenprime/article/details/7091224</a>
ActiveMQ使用的是jetty伺服器, 打開conf/jetty.xml檔案,找到
<bean id="securityConstraint" class="org.eclipse.jetty.http.security.Constraint">
</bean>
将property name為authenticate的屬性value="false" 改為"true",
控制台的登入使用者名密碼儲存在conf/jetty-realm.properties檔案中,内容如下:
## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements. See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License. You may obtain a copy of the License at
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
值得注意的是 使用者名和密碼的格式是
使用者名 : 密碼 ,角色名