Spring Boot + ActiveMQ示例
在本教程中,我们将看到如何创建Spring Boot + ActiveMQ示例。
Spring Boot附带嵌入式ActiveMQ类似于Tomcat,因此我们不必创建外部Activemq.if想要使用外部ActiveMQ进行配置,我们可以通过应用程序的更改进行配置。
以下是创建Spring Boot + ActiveMQ示例的步骤。
第1步:使用名为"springbootivemqexample"的Eclipse中的Maven创建一个简单的Java项目。
第2步:更改"pom.xml"如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.igi.theitroad</groupId>
<artifactId>SpringBootActiveMQExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
</dependencies>
<build>
<finalName>SpringBootActiveMQExample</finalName>
</build>
</project>
Spring-Boot-Starter-Parent为我们提供任何Spring项目所需的所有Maven默认值。
由于我们正在开发与ActiveMQ集成的Spring应用程序,因此我们还需要添加Spring-Boot-Starter-ActiveMQ依赖项。
这将包括其他依赖性此类应用程序所需的Spring Boot,ActiveMQ等。
第3步:在包中创建一个名为"messagecreator.java"的文件.igi.theitroad.springboot
package org.igi.theitroad.springboot;
import javax.jms.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
@Component
public class MessageCreator implements CommandLineRunner {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
public void run(String... arg0) throws Exception {
//This will put text message to queue
this.jmsMessagingTemplate.convertAndSend(this.queue, "Hello theitroad!!");
System.out.println("Message has been put to queue by sender");
}
}
第4步:在包中创建一个名为"messagereceiver.java"的文件.igi.theitroad.springboot
package org.igi.theitroad.springboot;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class MessageReceiver {
@JmsListener(destination = "theitroad.queue")
public void receiveQueue(String text) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Message Received: "+text);
}
}
@jmslistner:此注释实际上用于将方法标记为指定目标()上的JMS消息侦听器的目标。
第5步:在包/src/main /资源中创建名为"application.properties"的文件
spring.activemq.in-memory=true spring.activemq.pool.enabled=false
如果要使用外部Activemq,请使用以下应用程序.properties
spring.activemq.broker-url=tcp://localhost:61616
请注意,如果我们指定以上属性,则只需提供ActiveMQ代理URL,然后在Memory Embedded ActiveMQ中不会在内存中创建另一个。
第6步:在Package .org.igi.theitroad中创建名为"springbootapplicatiomain.java"的文件
package org.igi.theitroad;
import javax.jms.Queue;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
@SpringBootApplication
@EnableJms
public class SpringBootApplicatioMain {
public static void main(String[] args)
{
SpringApplication.run(SpringBootApplicatioMain.class, args);
}
@Bean
public Queue queue() {
return new ActiveMQQueue("theitroad.queue");
}
}
我们刚刚添加了@springbootapplication,它做了所有的工作。
让我们了解更多关于这个注释的信息。
@springBootApplication是添加以下所有内容的注释:
@configuration使类作为应用程序上下文的Bean定义的源。
@EnableAutoConfiguration允许Spring Boot在ClassPath设置和各种属性设置中添加Bean礼物。
将应用程序标记为Web应用程序,并激活诸如设置DispatcherServlet之类的关键行为。
@ComponentsCan告诉Spring在默认包中查找其他组件,配置和服务,允许它找到控制器。
如果未定义特定包,则会从声明此注释的类的包中扫描。
@EnableJms注释用于触发使用@JMSListener注释的方法的搜索,从而在背景中创建JMS侦听器。
运行上面的程序时,我们将得到以下输出:
2016-04-30 19:14:44.271 INFO 8757 — [ main] o.a.activemq.broker.TransportConnector : Connector vm://localhost started Message has been put to queue by sender 2016-04-30 19:14:44.438 INFO 8757 — [ main] o.a.theitroad.SpringBootApplicatioMain : Started SpringBootApplicatioMain in 2.852 seconds (JVM running for 3.548) Message Received: Hello theitroad!!

