Broker:英語有代理的意思,在activemq中,Broker就相當於一個Activemq實例。
1. 命令行啟動實例:
1.activemq start使用默認的activemq.xml啟動
E:\activemq\apache-activemq-5.15.6\bin>pwd /e/activemq/apache-activemq-5.15.6/bin E:\activemq\apache-activemq-5.15.6\bin>ls activemq activemq.bat win32 wrapper.jar activemq-admin.bat activemq.jar win64 E:\activemq\apache-activemq-5.15.6\bin>activemq.bat Java Runtime: Oracle Corporation 1.8.0_121 C:\Program Files\Java8\jdk1.8.0_121\jre Heap sizes: current=1005056k free=989327k max=1005056k JVM args: -Dcom.sun.management.jmxremote -Xms1G -Xmx1G -Djava.util.logging.config.file=logging.properties -Djava.se urity.auth.login.config=E:\activemq\apache-activemq-5.15.6\bin\..\conf\login.config -Dactivemq.classpath=E:\activemq\ap che-activemq-5.15.6\bin\..\conf;E:\activemq\apache-activemq-5.15.6\bin\../conf;E:\activemq\apache-activemq-5.15.6\bin\. /conf; -Dactivemq.home=E:\activemq\apache-activemq-5.15.6\bin\.. -Dactivemq.base=E:\activemq\apache-activemq-5.15.6\bin .. -Dactivemq.conf=E:\activemq\apache-activemq-5.15.6\bin\..\conf -Dactivemq.data=E:\activemq\apache-activemq-5.15.6\bi \..\data -Djava.io.tmpdir=E:\activemq\apache-activemq-5.15.6\bin\..\data\tmp Extensions classpath: [E:\activemq\apache-activemq-5.15.6\bin\..\lib,E:\activemq\apache-activemq-5.15.6\bin\..\lib\camel,E:\activemq\apache activemq-5.15.6\bin\..\lib\optional,E:\activemq\apache-activemq-5.15.6\bin\..\lib\web,E:\activemq\apache-activemq-5.15. \bin\..\lib\extra] ACTIVEMQ_HOME: E:\activemq\apache-activemq-5.15.6\bin\.. ACTIVEMQ_BASE: E:\activemq\apache-activemq-5.15.6\bin\.. ACTIVEMQ_CONF: E:\activemq\apache-activemq-5.15.6\bin\..\conf ACTIVEMQ_DATA: E:\activemq\apache-activemq-5.15.6\bin\..\data Usage: Main [--extdir <dir>] [task] [task-options] [task data] Tasks: browse - Display selected messages in a specified destination. bstat - Performs a predefined query that displays useful statistics regarding the specified brok r consumer - Receives messages from the broker create - Creates a runnable broker instance in the specified path. decrypt - Decrypts given text dstat - Performs a predefined query that displays useful tabular statistics regarding the specif ed destination type encrypt - Encrypts given text export - Exports a stopped brokers data files to an archive file list - Lists all available brokers in the specified JMX context producer - Sends messages to the broker purge - Delete selected destination's messages that matches the message selector query - Display selected broker component's attributes and statistics. start - Creates and starts a broker using a configuration file, or a broker URI. stop - Stops a running broker specified by the broker name. Task Options (Options specific to each task): --extdir <dir> - Add the jar files in the directory to the classpath. --version - Display the version information. -h,-?,--help - Display this help information. To display task specific help, use Main [task] -h,-?,--help Task Data: - Information needed by each specific task. JMX system property options: -Dactivemq.jmx.url=<jmx service uri> (default is: 'service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi') -Dactivemq.jmx.user=<user name> -Dactivemq.jmx.password=<password> E:\activemq\apache-activemq-5.15.6\bin>activemq.bat start Java Runtime: Oracle Corporation 1.8.0_121 C:\Program Files\Java8\jdk1.8.0_121\jre Heap sizes: current=1005056k free=989327k max=1005056k
啟動后訪問后台:
2.activemq start xbean:file:../conf/activemq2.xml 使用指定的配置文件進行啟動
1.我們把con目錄下的activemq2.xml重新命名為activemq2.xml
2.再次直接start啟動會報錯:
ERROR: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path
resource [activemq.xml]; nested exception is java.io.FileNotFoundException: class path resource [activemq.xml] cannot be
opened because it does not exist
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resourc
e [activemq.xml]; nested exception is java.io.FileNotFoundException: class path resource [activemq.xml] cannot be opened
because it does not exist
3.我們指定啟動的xml文件位置再次啟動可以啟動成功
activemq start xbean:file:../conf/activemq2.xml
3.如果不指定file,也就是xbean:activemq2.xml,那么activemq2.xml必須在classpath目錄下
2.用activemq來構建java應用---不依賴於ActiveMQ應用,只需要jar包即可實現
這里主要是用Activemq Broker作為獨立的消息服務器來構建Java應用。簡單的說,就是在java應用中啟動activemq。這種方式會以進程的方式啟動一個新的JVM來支持連接。
嵌入式Broker啟動
下面的啟動方式都不能通過http訪問連接,要想測試是否啟動成功只能通過收消息和發消息來測試。
1.通過BrokerService方式啟動
BrokerService brokerService = new BrokerService(); brokerService.setUseJmx(true); brokerService.addConnector("tcp://localhost:61616"); brokerService.start();
2.通過 BrokerFactory 啟動
String uri = "properties:broker.properties"; BrokerService broker = BrokerFactory.createBroker(new URI(uri)); broker.addConnector("tcp://localhost:61616"); broker.start();
broker.properties內容如下:
useJmx=true persistent=false brokerName=QQQ
當然上面的確定方式都有對應的整合Spring之后的啟動方式。
3.BrokerService方式整合spring啟動
單例模式的BrokerService,加載完成之后調用start方法即可。
<!--Broker啟動方式--> <bean id="brokerService" class="org.apache.activemq.broker.BrokerService" init-method="start" destroy-method="stop"> <property name="brokerName" value="broker1"/> <property name="persistent" value="false"/> <property name="transportConnectorURIs"> <list> <value>tcp://localhost:61616</value> </list> </property> </bean>
從上面也可以看出,一個Broker可以配置多個連接的URI,如下面配置:(端口必須不同)
<!--Broker啟動方式--> <bean id="brokerService" class="org.apache.activemq.broker.BrokerService" init-method="start" destroy-method="stop"> <property name="brokerName" value="broker1"/> <property name="persistent" value="false"/> <property name="transportConnectorURIs"> <list> <value>tcp://localhost:61616</value> <value>tcp://localhost:61618</value> </list> </property> </bean>
4.通過 BrokerFactory 結合spring啟動
spring的主配置文件:
<!--Broker啟動方式--> <bean id="brokerService" class="org.apache.activemq.xbean.BrokerFactoryBean"> <property name="config" value="activemq.xml"/> <property name="start" value="true"/> </bean>
activemq.xml位於classpath下,內容如下:
<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.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <!-- The <broker> element is used to configure the ActiveMQ broker. --> <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}"> <destinationPolicy> <policyMap> <policyEntries> <policyEntry topic=">" > <!-- The constantPendingMessageLimitStrategy is used to prevent slow topic consumers to block producers and affect other consumers by limiting the number of messages that are retained For more information, see: http://activemq.apache.org/slow-consumer-handling.html --> <pendingMessageLimitStrategy> <constantPendingMessageLimitStrategy limit="1000"/> </pendingMessageLimitStrategy> </policyEntry> </policyEntries> </policyMap> </destinationPolicy> <!-- The systemUsage controls the maximum amount of space the broker will use before disabling caching and/or slowing down producers. For more information, see: http://activemq.apache.org/producer-flow-control.html --> <systemUsage> <systemUsage> <storeUsage> <storeUsage limit="100 gb"/> </storeUsage> <tempUsage> <tempUsage limit="50 gb"/> </tempUsage> </systemUsage> </systemUsage> <transportConnectors> <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB --> <transportConnector name="openwire" uri="tcp://localhost:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/> </transportConnectors> </broker> </beans>
3.ActiveMQ的Broker方式啟動多個broker的方法
1.復制conf文件夾並重新命名為conf2
2.修改conf2文件下的activemq.xml
修改brokerName
修改持久化的數據目錄
修改transportConnector的端口,要與第一個默認的不同
<transportConnectors> <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB --> <transportConnector name="openwire" uri="tcp://0.0.0.0:61617?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/> </transportConnectors>
3.修改conf2\jetty.xml,主要是改端口
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start"> <!-- the default port number for the web console --> <property name="host" value="0.0.0.0"/> <property name="port" value="8162"/> </bean>
4.復制bin目錄下面的activemq.bat並命名為activemq2.bat(如果是linux操作系統復制activemq)
5.修改activemq2.bat中的配置文件目錄:
6.啟動兩個broker (如果是linux還需要賦予上面復制后的文件可執行權限 chmod +x activemq2)
activemq2.bat start 和 activemq.bat start
7.http界面查看jetty服務器