ActiveMQ——Broker

一、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&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="amqp"
                                uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="stomp"
                                uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="mqtt"
                                uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="ws"
                                uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;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();
        }
    }
}

 消费者端也是一样,将连接地址换成内嵌的服务地址接口,但是需要注意一定要先启动内嵌的服务

好的,你想了解如何使用Java连接ActiveMQ并查看topic消息,可以按照以下步骤操作: 1. 下载并安装ActiveMQ。可以从官网下载最新的ActiveMQ安装包,并按照安装向导进行安装。 2. 创建Java项目并导入ActiveMQ相关的jar包。可以使用Maven管理项目依赖,添加以下依赖: ```xml <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.16.2</version> </dependency> ``` 3. 创建连接和会话。可以使用以下代码创建连接和会话: ```java // 创建连接工厂 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); // 创建连接 Connection connection = connectionFactory.createConnection(); // 启动连接 connection.start(); // 创建会话 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ``` 4. 创建topic和消息消费者。可以使用以下代码创建topic和消息消费者: ```java // 创建topic Topic topic = session.createTopic("test.topic"); // 创建消息消费者 MessageConsumer consumer = session.createConsumer(topic); ``` 5. 接收消息。可以使用以下代码接收消息: ```java // 接收消息 Message message = consumer.receive(); if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; System.out.println("Received message: " + textMessage.getText()); } ``` 这样就可以使用Java连接ActiveMQ并查看topic消息了。当然,还有其他更详细的操作可以参考ActiveMQ官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值