Spring Integration & ActiveMQ


Spring Integration & ActiveMQ

Ten wpis ma na celu zademonstrowanie przykładowej aplikacji z użyciem Spring Boota która za pomocą modułu Spring Integration pozwoli nasłuchiwać komunikaty w kolejce ActiveMQ. Do dzieła! Tworzymy nowy projekt Spring Boota – plik pom.xml – niezbędne zależności:

<properties>
	<java.version>1.8</java.version>
</properties>
 
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
 
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jms</artifactId>
	</dependency>
 
	<dependency>
		<groupId>org.springframework.integration</groupId>
		<artifactId>spring-integration-jms</artifactId>
	</dependency>
 
	<dependency>
		<groupId>org.apache.activemq</groupId>
		<artifactId>activemq-broker</artifactId>
	</dependency>
 
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
 
	<dependency>
		<groupId>org.codehaus.groovy</groupId>
		<artifactId>groovy-all</artifactId>
		<version>2.4.4</version>
		<scope>test</scope>
	</dependency>
 
	<dependency>
		<groupId>org.spockframework</groupId>
		<artifactId>spock-core</artifactId>
		<version>1.2-groovy-2.4</version>
		<scope>test</scope>
	</dependency>
 
	<dependency>
		<groupId>org.spockframework</groupId>
		<artifactId>spock-spring</artifactId>
		<version>1.2-groovy-2.4</version>
		<scope>test</scope>
	</dependency>
</dependencies>
 
<build>
	<plugins>
		<plugin>
			<groupId>org.codehaus.gmavenplus</groupId>
			<artifactId>gmavenplus-plugin</artifactId>
			<version>1.6</version>
			<executions>
				<execution>
					<goals>
						<goal>compileTests</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

Plik ./resources/context-config.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jms="http://www.springframework.org/schema/integration/jms"
       xmlns:integration="http://www.springframework.org/schema/integration"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">
 
    <jms:message-driven-channel-adapter connection-factory="jmsConnectionFactory" id="jmsAdapter" destination="javaLeaderQueue" channel="JavaLeaderChannel"/>
    <integration:channel id="javaLeaderChannel"/>
    <integration:service-activator id="javaLeaderServiceActivator" input-channel="JavaLeaderChannel" ref="javaLeaderService" method="printMsg"/>
 
</beans>

Plik application.properties:

spring.activemq.in-memory  = true
spring.activemq.pooled     = false
spring.activemq.broker-url = failover://tcp://localhost:61616
spring.activemq.user       = admin
spring.activemq.password   = admin

Klasa konfiguracyjna – ActiveMQConfig :

@Configuration
public class ActiveMQConfig {
 
    public static final String JAVA_LEADER_QUEUE = "javaLeaderQueue";
    @Bean
    public Queue javaLeaderQueue() {
        return new ActiveMQQueue(JAVA_LEADER_QUEUE);
    }
}

Usługa – JavaLeaderService bean:

@Service
public class JavaLeaderService {
    public void printMsg(String msg){
        System.out.println("################################");
        System.out.println("##  JavaLeader " + msg + "!!!"   );
        System.out.println("################################");
    }
}

Klasa startowa:

@SpringBootApplication
@ImportResource("classpath*:/context-config.xml")
public class SpringIntegrationActiveMqSpringBootApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringIntegrationActiveMqSpringBootApplication.class, args);
	}
}

Zanim zostaną uruchomione testy należy pobrać brokera ActiveMQ:

https://activemq.apache.org/download.html

uruchamiamy:

$ACTIVEMQ_HOME/bin/activemq start

Testy w języku groovytworzymy fabrykę połączeń oraz testujemy wysłanie i odbiór komunikatu:

@ContextConfiguration(loader = SpringBootContextLoader.class, classes = [SpringIntegrationActiveMqSpringBootApplication])
class JavaLeaderServiceTest extends Specification{
 
    @Autowired
    @Qualifier("jmsConnectionFactory")
    ConnectionFactory connectionFactory
 
    String queueName = ActiveMQConfig.JAVA_LEADER_QUEUE
    Session session
    Destination destination
    MessageProducer producer
 
    def setup(){
        Connection conn = connectionFactory.createConnection()
        conn.start()
        session       = conn.createSession(false, Session.AUTO_ACKNOWLEDGE)
        destination   = session.createQueue(queueName)
        this.producer = session.createProducer(destination)
        this.producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT)
    }
 
    def "Test Send and Receive of JavaLeader Message"() {
        given:
            TextMessage txtMessage = session.createTextMessage()
            txtMessage.setText("JavaLeader.pl")
        when:
            producer.send(destination, txtMessage)
            sleep(3000)
            then:
            true
    }
}

Uruchamiamy testy i przechodzimy do konsoli ActiveMQ:

http://localhost:8161/admin

login: admin

hasło: admin

dane te zdefiniowane są w pliku application.properties.

wynik wykonania testów:

Started JavaLeaderServiceTest in 12.441 seconds (JVM running for 15.23)
o.a.a.t.failover.FailoverTransport:Successfully connected to tcp://localhost:61616
 
################################
##  JavaLeader JavaLeader.pl!!!
################################

Zobacz kod na GitHubie i zapisz się na bezpłatny newsletter!

.


Leave a comment

Your email address will not be published.


*