activeMQ各種配置
一.設定ConnectionFactory
1.首先CachingConnectionFactory和SingleConnectionFactory是spring提供的PooledConnectionFactory是activemq提供的
2.SingleConnectionFactory是單例的所有連線共享同一個Connection,但是session不共享,並且會忽略Connection的close方法呼叫.
3.CachingConnectionFactory是繼承SingleConnectionFactory,所以它擁有SingleConnectionFactory的所有功能,同時它還新增了快取功能,它可以快取Session、MessageProducer和MessageConsumer
4.PooledConnectionFactory是activemq提供的,這個類只會快取connection,session和productor,不會快取consumer,activemq認為,由於消費者一般是非同步的,也就是說,broker代理會把生產者傳送的訊息放在一個訊息者的預取快取中。當訊息者準備好的時候就會從這個預取快取中取出來進行處理。
5.訊息處理的及時性不是特別高的情況下用PooledConnectionFactory,因為它不快取consumer,如果希望處理能夠提高速度,自然也可以用CachingConnectionFactory,從這部分提高效率,減小不斷建立consumer的時間
二.設定prefetchSize和optimizeAcknowledge
1.optimizeAcknowledge是批量向broker確認消費訊息(延遲確認),如果為false就是每消費一條訊息都會去確認,為true當消費數大於prefetchSize*0.65就會批量確認消費,可以節省多次確認消費的時間,提高響應效率
但是這種機制有缺陷,如果在已經消費一部分但是還沒有確認消費,這時activemq掛了,會導致訊息重複消費.
不想重複消費就關閉optimizeAcknowledge,可是每次請求都會確認一下,效能較低,
prefetchSize每次給消費方推多少訊息,它不僅減少了client端在獲取訊息時阻塞的次數和阻塞的時間,還能夠大大的減少網路開支.
prefetchSize的設定根據情況設定
修改密碼(後臺服務端連線密碼)
1.在mq伺服器上的activemq.xml找到<systemUsage>,在其上方新增如下外掛
<!-- set connection user authentication-->
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="${activemq.username}" password="${activemq.password}" groups="users,admins"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
2.開啟**\apache-activemq\conf中credentials.properties檔案進行密碼設定
activemq.username=admin
activemq.password=123456
guest.password=123456
3.在springboot工程中僅需要在application.properties檔案中新增一下配置即可
spring.activemq.user=admin
spring.activemq.password=123456
修改密碼(前臺瀏覽器連線密碼)
1.開啟**\apache-activemq\conf中jetty-realm.properties檔案進行密碼設定
admin: admin, admin
user: 123, user
說明:上面有兩個賬戶,分別是admin和user.第一個admin是使用者名稱,第二個admin是密碼,第三個admin是所在是組.設定好了,就可以在瀏覽器驗證.
持久化到資料庫(Queue模式)
1.配置activemq.xml修改persistenceAdapter
<persistenceAdapter>
<!-- <kahaDB directory="${activemq.data}/kahadb"/> -->
<!-- createTablesOnStartup:default value is true; if true then every time start will create table;so first time we set it be true;others be false -->
<jdbcPersistenceAdapter dataSource="#activemq-ds" createTablesOnStartup="true" />
</persistenceAdapter>
2.在</broker>後面加上
<bean id="activemq-ds" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.17.128:3306/activemq?relaxAutoCommit=true"/>
<property name="username" value="xc"/>
<property name="password" value="root"/>
<property name="maxActive" value="200"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
持久化到資料庫(Topic模式)
需要在程式碼中進行
客戶端接受訊息端(jmsListener)
//建立activeMQConnectionFactory
@Bean
public ActiveMQConnectionFactory activeMQConnectionFactory() {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
activeMQConnectionFactory.setBrokerURL(brokerUrl);
activeMQConnectionFactory.setUserName(user);
activeMQConnectionFactory.setPassword(password);
return activeMQConnectionFactory;
}
// 建立CachingConnectionFactory
@Bean
@Primary
public CachingConnectionFactory connectionFactorys1() {
CachingConnectionFactory c = new CachingConnectionFactory();
c.setTargetConnectionFactory(activeMQConnectionFactory());
c.setSessionCacheSize(100);
c.setClientId("111");
return c;
}
// 設定topice模型的監聽ConnectionFactory
@Bean(name="xc1")
@Primary
public JmsListenerContainerFactory<?> jmsListenerContainerTopic1() {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
//設定訂閱/釋出模式監聽
bean.setPubSubDomain(true);
bean.setConnectionFactory(connectionFactorys1());
//設定可以持久化訂閱
bean.setSubscriptionDurable(true);
//設定客戶端ID
bean.setClientId("111");
return bean;
}
注意:這裡的紅色處必須設定一樣,但每個監聽的持久化訂閱的ClientId必須不一樣,否則會報錯.因為他在持久化中要表示這是那個客戶端.如下圖,資料庫顯示
服務端傳送訊息端
@Bean(name ="jmsTemplatetopic")
public JmsTemplate jmsTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactorys1());
//可以不設定,預設是持久化
jmsTemplate.setDeliveryMode(DeliveryMode.PERSISTENT);
//設定為訂閱/釋出模式
jmsTemplate.setPubSubDomain(true);
//必須開啟,不然持久化設定無效
jmsTemplate.setExplicitQosEnabled(true);
return jmsTemplate;
}