ActiveMQ内置Broker启动方式

一、前言

ActiveMQ除了可以作为独立进程单独部署在服务器上之外,也可以很小巧的内嵌在程序中启动,下面我们来简单的介绍内置Broker启动的几种方式。
首先准备工作还是需要准备好activemq的jar包,请自行配置,不多赘述

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.15.2</version>
        </dependency>

一、代码直接启动

这种方式直接在代码中启动,因为比较简单,就直接上代码了:

import org.apache.activemq.broker.BrokerService;

/**
 * Created with IntelliJ IDEA.
 * User: Roy
 * Date: 2017/12/11 0011
 * Time: 14:15
 * Description:
 */
public class InnerBroker {
    public static void main(String args[]) throws Exception{
        BrokerService broker = new BrokerService();
        //启用broker的JMX监控功能
        broker.setUseJmx(true);
        //设置broker名字
        broker.setBrokerName("MyBroker");
        //是否使用持久化
        broker.setPersistent(false);
        //添加连接协议,地址
        broker.addConnector("tcp://localhost:61616");
        broker.start();
    }
}

运行后,可以再通过sender和receiver测试,写法请参照上一篇文章(http://blog.csdn.net/Roy_70/article/details/78731729
这样的话,最基本的内嵌Broker就启动完成了。

二、工厂模式启动

这种类似代码直接启动,只不过通过工厂模式读取配置文件,再启动。所以需要在resources目录或者项目根目录下新建一个properties文件,这里就叫broker.properties吧,内容是:

useJms=true
persistent=false
brokerName=FactoryBroker

内容就跟代码启动的参数一样,代码是:

import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import java.net.URI;

/**
 * Created with IntelliJ IDEA.
 * User: Roy
 * Date: 2017/12/11 0011
 * Time: 14:22
 * Description:
 */
public class InnerFactoryBroker {
    public static void main(String args[]) throws Exception{
        String uri = "properties:broker.properties";
        BrokerService broker = BrokerFactory.createBroker(new URI(uri));
        broker.addConnector("tcp://localhost:61616");
        broker.start();
    }
}

这种方式可以通过配置文件更改部分参数

三、Spring启动

Spring是非常强大好用的工具,我们都喜欢将一些配置或者固定启动的东西放在里面,ActiveMQ也不例外。使用Spring首先需要记在依赖:

<!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>

        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>

然后创建配置xml文件,这里我们建一个beans.xml文件:

<?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.xsd">
    <bean id="broker" class="org.apache.activemq.broker.BrokerService" init-method="start" destroy-method="stop">
        <property name="brokerName" value="SpringBroker"/>
        <property name="persistent" value="false"/>
        <property name="transportConnectorURIs">
            <list>
                <value>tcp://localhost:61616</value>
            </list>
        </property>
    </bean>
</beans>

里面也是配置broker启动的一些基本参数,然后配置都在spring内,代码就很简单了:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created with IntelliJ IDEA.
 * User: Roy
 * Date: 2017/12/11 0011
 * Time: 14:33
 * Description:
 */
public class SpringBroker {
    public static void main(String args[]){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    }
}

以上就是是那种简单的内嵌ActiveMQ启动方式,其实通过Spring也可以再用factory的方式启动,有时间的话再加上吧。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值