1. 程式人生 > 程式設計 >mybatis多資料來源動態切換的完整步驟

mybatis多資料來源動態切換的完整步驟

Spring整合ActiveMQ

前面三節講了ActiveMQ的安裝、測試。JMS介紹以及四大組成元素,還有ActiveMQ的可靠性(持久化、事務、簽收),還有Broker等等。

但是光學會還不夠,我們還要把ActiveMQ應用到我們已有的框架中,如Spring和SpringBoot等等。

這一節主要學習如何在Spring中使用ActiveMQ。

環境配置

首先建立一個spring專案,在pom檔案中匯入我們需要的jar包

<!-- activemq所需要的jar包 -->
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-all</artifactId>
			<version>5.15.9</version>
		</dependency>
		<dependency>
			<groupId>org.apache.xbean</groupId>
			<artifactId
>
xbean-spring</artifactId> <version>4.17</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.3</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
<!-- activeMQ對JMS的支援,整合Spring和ActiveMQ --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>5.2.10.RELEASE</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.15.9</version> </dependency>

然後匯入Spring必須的jar包。

Spring整合ActiveMQ之佇列生產者

  1. 配置ApplicationContext.xml檔案
<!-- 自動掃描 -->
	<context:component-scan base-package="com.zxg.activemq"></context:component-scan>
	
	<!-- 配置生產者 -->
	<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
		<property name="connectionFactory">
			<!-- 真正可以生產Connection的ConnectionFactory,對應的JMS服務廠商提供 -->
			<bean class="org.apache.activemq.ActiveMQConnectionFactory">
				<!--  tcp://自己的伺服器地址:埠-->
				<property name="brokerURL" value="tcp://129.*.*.*:61616"></property>
			</bean>
		</property>
		<property name="maxConnections" value="100"></property>
	</bean>
	
	<!-- 這個是佇列目的地,點對點的 -->
	<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg index="0" value="spring-active-queue"></constructor-arg>
	</bean>
	
	<!-- Spring提供的JMS工具類,他可以進行訊息傳送,接收等 -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="jmsFactory"></property>
 		<property name="defaultDestination" ref="destinationQueue"></property>
 		<property name="messageConverter" >
 			<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"></bean>
 		</property>
	</bean>
  1. 配置Service層
public interface SpringMQ_Produce {
	void send(String messageStr);
}
@Service("springMQ_Produce")
public class SpringMQ_ProduceImpl implements SpringMQ_Produce{

	@Autowired
	private JmsTemplate jmsTemplate;
	
	public void send(String messageStr) {
		@SuppressWarnings("resource")
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

		SpringMQ_ProduceImpl produce = (SpringMQ_ProduceImpl) ctx.getBean("springMQ_Produce");
		
		produce.jmsTemplate.send((session)->{
			TextMessage textMessage = session.createTextMessage(messageStr);
			return textMessage;
		});
		System.out.println("send success");
	}
}
  1. 測試
public class TestProduceService {
	
	private SpringMQ_Produce springMQ_Produce;
	ApplicationContext ctx;
	{
		ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		springMQ_Produce = (SpringMQ_Produce) ctx.getBean("springMQ_Produce");
	}
	
	@Test
	public void test() {
		springMQ_Produce.send("Spring 整合ActiveMQ!!!");
	}

}
  • 執行結果
    然後檢視MQ伺服器控制檯
    發現訊息已經發布成功了。

Spring整合ActiveMQ之佇列消費者

為了方便,就直接在Service層實現測試

@Service
public class SpringMQ_Consumer {
	
	@Autowired
	private JmsTemplate jmsTemplate;
	
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		
		SpringMQ_Consumer consumer = (SpringMQ_Consumer) ctx.getBean("springMQ_Consumer");
		String re = (String) consumer.jmsTemplate.receiveAndConvert();
		System.out.println(re);
	}

}

檢視執行結果,正好是生產者釋出的訊息,並且訊息隊列出隊發生變化。

Spring整合ActiveMQ之主題生產消費

首先修改配置檔案applicationContext.xml

	<!-- 主題,一對多 -->
	<bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
		<constructor-arg index="0" value="spring-active-topic"></constructor-arg>
	</bean>
	
	<!-- Spring提供的JMS工具類,他可以進行訊息傳送,接收等 -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="jmsFactory"></property>
 		<!-- <property name="defaultDestination" ref="destinationQueue"></property> -->
 		<property name="defaultDestination" ref="destinationTopic"></property>
 		<property name="messageConverter" >
 			<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"></bean>
 		</property>
	</bean>

然後消費者與生產者的程式碼不用改,我們的配置資訊全在applicationContext.xml中。

首先啟動消費者進行監聽
再啟動生產者釋出訊息。

Spring整合ActiveMQ之監聽器配置

實現在spring裡面實現消費者不啟動,直接通過配置監聽完成。

簡單一點,就是把之前消費者中的監聽器程式碼改為使用一個監聽類來實現即可。

首先更改配置檔案applicationContext.xml

<!-- 配置監聽程式 -->
	<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="jmsFactory"></property>
		<property name="destination" ref="destinationQueue"></property>
		<!-- public class myMessageListener implements MessageListener -->
		<property name="messageListener" ref="myMessageListener"></property>
	</bean>

然後寫我們的監聽類

@Component
public class MyMessageListener implements MessageListener{

	@Override
	public void onMessage(Message message) {
		if(null!=message && message instanceof TextMessage) {
			TextMessage textMessage = (TextMessage) message;
			System.out.println("textMessage:"+textMessage);
		}
	}
}

現在測試
只需啟動我們的生產者,檢視執行結果: