ActiveMQ - 启动JMS服务

1. 依赖 jar

activemq-all-5.6.0.jar
jackson-all-1.8.5.jar
xstream-1.4.2.jar

2. Java 代码

    /**
     * 启动 JMS 服务
     * @param args 参数
     */
    public static void main(String[] args) throws Exception
    {
        BrokerService broker = new BrokerService();
        broker.setBrokerId("broker1");
        broker.setBrokerName("localhost1");
        broker.addConnector("tcp://0.0.0.0:61616");   // 开启 tcp   协议
        broker.addConnector("stomp://0.0.0.0:61613"); // 开启 stomp 协议
        setJmsUserInfo(broker);                       // 设置用户信息
        broker.start();
        
        System.out.println("-- start JMS Server success. --");
    }
    
    /**
     * 设置JMS用户信息
     * @param broker broker
     */
    private static void setJmsUserInfo(BrokerService broker)
    {
        AuthenticationUser user = new AuthenticationUser("user1", "password1", "group1");
        List<AuthenticationUser> users = new ArrayList<AuthenticationUser>();
        users.add(user);
        
        SimpleAuthenticationPlugin auth = new SimpleAuthenticationPlugin();
        auth.setUsers(users);
        
        BrokerPlugin[] plugins = new BrokerPlugin[]{auth};        
        broker.setPlugins(plugins);
    }

public class JMSInfo
{
    // tcp 协议使用点对点模式时, 队列名称不需要前缀
    public static final String QUEUE_NAME = "test_queue";
    
    // tcp 协议使用发布/订阅模式时, 主题名称不需要前缀
    public static final String TOPIC_NAME = "test_topic";
    
    // stomp 协议使用点对点模式时, 需要有前缀 /queue/
    public static final String STOMP_QUEUE_NAME = "/queue/" + QUEUE_NAME;
    
    // stomp 协议使用发布/订阅模式时, 需要有前缀 /topic/
    public static final String STOMP_TOPIC_NAME = "/topic/" + TOPIC_NAME;
}


你可能感兴趣的:(activemq)