ActiveMQ(十一)--Broker的启动方式

Broker:相当于一个ActiveMQ服务器实例

命令行启动参数实例:
1. ./activemq start:使用默认的activemq.xml来启动
2. ./activemq start xbean:file:../conf/activemq-2.xml   使用指定的配置文件来启动
3.如果不指定file,也就是xbean:activemq-2.xml,那么xml必须在classpath下面

用ActiveMQ来构建Java应用

这里主要讲用ActiveMQ Broker作为独立的消息服务器来构建Java应用。ActiveMQ也支持在vm中通信基于嵌入式的Broker,能够无缝的集成其它Java应用。

嵌入式Broker启动
1.Broker service启动broker

import org.apache.activemq.broker.BrokerService;

public class InnerBroker {
    public static void main(String[] args) throws Exception {
        BrokerService broker = new BrokerService();
        broker.setUseJmx(true);
        broker.addConnector("tcp://localhost:61616");
        broker.start();
    }
}

2.BrokerFactory启动broker

import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;

import java.net.URI;

public class InnerBroker {
    public static void main(String[] args) throws Exception {
//        BrokerService broker = new BrokerService();
//        broker.setUseJmx(true);
//        broker.addConnector("tcp://localhost:61616");
//        broker.start();

        String uri = "properties:broker.properties";
        BrokerService broker1 = BrokerFactory.createBroker(new URI(uri));
        broker1.addConnector("tcp://localhost:61616");
        broker1.start();

    }
}

broker.properties的内容

useJmx=true
persistent=false
brokerName=Cheese

3.利用spring集成broker

applicationContext.xml




    
        
        
        
            
                tcp://localhost:61616
            
        
    

启动文件

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

public class InnerBroker {
    public static void main(String[] args) throws Exception {
//        BrokerService broker = new BrokerService();
//        broker.setUseJmx(true);
//        broker.addConnector("tcp://localhost:61616");
//        broker.start();


//        String uri = "properties:broker.properties";
//        BrokerService broker1 = BrokerFactory.createBroker(new URI(uri));
//        broker1.addConnector("tcp://localhost:61616");
//        broker1.start();

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");


    }
}

只要加载一下spring的配置文件,内嵌的broker就启动了。

 

ActiveMQ的启动:

1.可以通过在应用程序中以编码的方式启动broker,例如:borker.start();

如果需要启动多个broker,那么需要为broker设置一个名字。例如:

BrokerService broker = new BrokerService();

broker.setName("fred");

broker.addConnector("tcp://localhost:61616");

broker.start();

2.还可以通过spring来启动。

你可能感兴趣的:(ActiveMQ)