日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ActiveMQ实战篇之 java和spring xml创建Broker(一)

發布時間:2024/2/28 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ActiveMQ实战篇之 java和spring xml创建Broker(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ActivityMQ創建broker是直接通過配置IOC注入的,所以了解如何用純JAVA代碼和spring xml 創建broker可以讓我們對AcitivtyMQ有一個更深入的了解


原本的配置

<broker xmlns="http://activemq.apache.org/schema/core" brokerName="myBroker" dataDirectory="${activemq.base}/data"><transportConnectors><transportConnector name="openwire"uri="tcp://localhost:61616" /></transportConnectors><plugins><simpleAuthenticationPlugin><users><authenticationUser username="admin"password="password"groups="admins,publishers,consumers"/><authenticationUser username="publisher"password="password"groups="publishers,consumers"/><authenticationUser username="consumer"password="password"groups="consumers"/><authenticationUser username="guest"password="password"groups="guests"/></users></simpleAuthenticationPlugin></plugins> </broker>

用純JAVA實現

BrokerService broker =new BrokerService();broker.setBrokerName("myBroker");broker.setDataDirectory("data/");SimpleAuthenticationPlugin authentication = new SimpleAuthenticationPlugin();List<AuthenticationUser> users = new ArrayList<AuthenticationUser>();users.add(new AuthenticationUser("admin", "password", "admins,publishers,consumers"));users.add(new AuthenticationUser("publisher", "password", "publishers,consumers"));users.add(new AuthenticationUser("consumer", "password", "consumers"));users.add(new AuthenticationUser("guest", "password", "guests"));authentication.setUsers(users);broker.setPlugins(new BrokerPlugin[] { authentication });broker.addConnector("tcp://localhost:61616");broker.start();System.out.println();System.out.println("Press any key to stop the broker");System.out.println();System.in.read();

可以實現同樣的功能,但是運行之后我們可以看到tcp端口61616開啟用于監聽,但是,和配置實現不同的是,純java實現并沒有開啟服務器,于是我們再模仿開啟服務器操作。

HTTPDiscoveryAgent httpDiscoveryAgent = new HTTPDiscoveryAgent(); httpDiscoveryAgent.setStartEmbeddRegistry(true); httpDiscoveryAgent.setRegistryURL("http://localhost:8161"); System.out.println(httpDiscoveryAgent.getRegistryURL()); httpDiscoveryAgent.start(); broker.addService(httpDiscoveryAgent);

只需要在broker.start前面加上這段代碼即可,這段具體怎么做?需要進行源碼搜索源碼分析才能知道為什么這么做,當然有時候需要打斷點查看哪一段代碼有錯誤或者缺了什么包。

需要這些包

這樣jetty服務器能夠正常開啟了


用spring xml實現

在項目的根目錄上加上spring配置文件

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" 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-2.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <amq:broker brokerName="localhost" dataDirectory="${activemq.base}/data"><amq:transportConnectors><amq:transportConnector name="openwire"uri="tcp://localhost:61616" /></amq:transportConnectors><amq:plugins><amq:simpleAuthenticationPlugin><amq:users><amq:authenticationUser username="admin"password="password"groups="admins,publishers,consumers"/><amq:authenticationUser username="publisher"password="password"groups="publishers,consumers"/><amq:authenticationUser username="consumer"password="password"groups="consumers"/><amq:authenticationUser username="guest"password="password"groups="guests"/></amq:users></amq:simpleAuthenticationPlugin></amq:plugins></amq:broker> </beans>

添加一個Factory,負責創建Broker

package com.tgb.activemq;import java.net.URI;import org.apache.activemq.broker.BrokerFactory; import org.apache.activemq.broker.BrokerService; import org.apache.activemq.xbean.XBeanBrokerFactory;public class Factory {public static void main(String[] args) throws Exception {System.setProperty("activemq.base", System.getProperty("user.dir"));String configUri = "xbean:Spring-simgle.xml";URI brokerUri = new URI(configUri);BrokerService broker = BrokerFactory.createBroker(brokerUri);broker.start();System.out.println();System.out.println("Press any key to stop the broker");System.out.println();System.in.read();} }

xbean:前綴是告訴Factory使用 XBeanBrokerFactory創建Broker


總結

以上是生活随笔為你收集整理的ActiveMQ实战篇之 java和spring xml创建Broker(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。