一、Broker简介
ActiveMQ的Broker其实就代表着ActiveMQ的服务,我们在启动ActiveMQ的时候会读取ActiveMQ的配置文件activemq.xml,默认的配置如下:
<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">
<!-- Allows us to use system properties as variables in this configuration file -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>file:${activemq.conf}/credentials.properties</value>
</property>
</bean>
<!-- Allows accessing the server log -->
<bean id="logQuery" class="io.fabric8.insight.log.log4j.Log4jLogQuery"
lazy-init="false" scope="singleton"
init-method="start" destroy-method="stop">
</bean>
<!--
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=">">
<pendingMessageLimitStrategy>
<constantPendingMessageLimitStrategy limit="1000"/>
</pendingMessageLimitStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
<managementContext>
<managementContext createConnector="false"/>
</managementContext>
<persistenceAdapter>
<kahaDB directory="${activemq.data}/kahadb"/>
</persistenceAdapter>
<systemUsage>
<systemUsage>
<memoryUsage>
<memoryUsage percentOfJvmHeap="70"/>
</memoryUsage>
<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://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp"
uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp"
uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt"
uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws"
uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
<!-- destroy the spring context on shutdown to stop jetty -->
<shutdownHooks>
<bean xmlns="http://www.springframework.org/schema/beans"
class="org.apache.activemq.hooks.SpringContextHook"/>
</shutdownHooks>
</broker>
<import resource="jetty.xml"/>
</beans>
<!-- END SNIPPET: example -->
该配置文件中配置了一个Broker对象的参数,ActiveMQ就会按照这些参数创建这个Broker。
我们在启动ActiveMQ服务的时候也可以指定配置文件:这样我们就可以在一台机器上启动多个MQ服务(但是要修改端口)
./activemq start xbean:file/myactivemq/apache-activemq-5.15.9/activemq01.xml
二、在Java程序中内嵌消息服务
除了启动ActiveMQ为我们提供的Broker,我们也可以通过在Java程序中创建Broker来搭建简易的ActiveMQ服务,将消息服务嵌入在我们自己的程序中,这种方式可以在ActiveMQ宕机时临时使用。
内嵌Broker服务:
public class MyBroker {
public static void main(String args[]) throws Exception {
BrokerService brokerService = new BrokerService();
brokerService.setUseJmx(true);
brokerService.addConnector("tcp://localhost:61616");
brokerService.start();
}
}
生产者和消费者可以连接我们的这个内嵌的消息服务:只需要将连接的地址改为我们内嵌服务的地址即可
public class MQProducer {
//内嵌MQ服务地址
private static final String MQ_URL = "tcp://localhost:61616";
private static final String QUERE_NAME = "queue01";
public static void main(String args[]) throws JMSException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(MQ_URL);
Connection connection = factory.createConnection();
connection.start();
//创建session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(QUERE_NAME);
//创建消息生产者
MessageProducer producer = session.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
try {
for (int i = 0; i < 3; i++) {
TextMessage textMessage = session.createTextMessage("msg" + i);
producer.send(textMessage);
}
} catch (Exception e) {
} finally {
producer.close();
session.close();
connection.close();
}
}
}
消费者端也是一样,将连接地址换成内嵌的服务地址接口,但是需要注意一定要先启动内嵌的服务