hjr-JAVA工作日记(四):Spring-boot和MQ-ActiveMQ +JMS

来源:互联网 发布:js 定义对象属性 编辑:程序博客网 时间:2024/05/23 13:16

Spring-boot

spring-boot可以简化配置文件
首先在pom中写

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http0://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>xh</groupId>  <artifactId>xh</artifactId>  <version>0.0.1-SNAPSHOT</version>  <name>xh Maven Webapp</name>  <url>http://maven.apache.org</url>     <!-- Inherit defaults from Spring Boot -->          <parent>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-parent</artifactId>            <version>1.5.4.RELEASE</version>        </parent>    <!-- Add typical dependencies for a web application -->      <dependencies>          <!-- spring-web开发相关依赖 -->         <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-web</artifactId>          </dependency>          <!-- spring-boot 测试 -->         <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-test</artifactId>              <scope>test</scope>          </dependency>         <!-- mongodb相关依赖 -->         <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-mongodb</artifactId>          </dependency> <!--        MQ -->        <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-activemq</artifactId>          </dependency>      </dependencies>      <!-- Package as an executable jar -->      <build>          <plugins>              <plugin>                  <groupId>org.springframework.boot</groupId>                  <artifactId>spring-boot-maven-plugin</artifactId>              </plugin>          </plugins>      </build>      <!-- Add Spring repositories -->      <!-- (you don't need this if you are using a .RELEASE version) -->      <repositories>          <repository>              <id>spring-snapshots</id>              <url>http://repo.spring.io/snapshot</url>              <snapshots><enabled>true</enabled></snapshots>          </repository>          <repository>              <id>spring-milestones</id>              <url>http://repo.spring.io/milestone</url>          </repository>      </repositories>      <pluginRepositories>          <pluginRepository>              <id>spring-snapshots</id>              <url>http://repo.spring.io/snapshot</url>          </pluginRepository>          <pluginRepository>              <id>spring-milestones</id>              <url>http://repo.spring.io/milestone</url>          </pluginRepository>      </pluginRepositories>  </project>  

与普通的pom相比,多了一个parent,写了这个其余所有jar包的版本都会自动配置,更方便
如果想用spring开发web,不再需要像以前那样写很多jar包,一个

   <!-- spring-web开发相关依赖 -->         <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-web</artifactId>          </dependency>  

就把WEB开发相关的jar包都包含了,其余像mongodb、maven也都同理。

spring-boot的项目有一个统一的入口文件,Application

@SpringBootApplication   public class Application {      public static void main(String[] args) {          SpringApplication.run(Application.class, args);      }  }  

整个项目在这里开始,
路由的配置也很简单,都是自动的。不需要再在spring-mvc.xml中配置扫描了

@RestController  @EnableAutoConfiguration  public class UserController{    @RequestMapping("/")      String home() {          return "Hello World!";      }            @RequestMapping("/hello/{myName}")      String index(@PathVariable String myName) {          return "Hello "+myName+"!!!";      }}  

使用@RestController全文都设置成json了,不用一个一个设置了。@EnableAutoConfiguration设置路由自动扫描,什么也不用配置了。运行时可以在自己的tomcat里添加改项目运行,或者仅仅右击Application然后run as java application,在通过localhost:8080/项目名访问。如果有依赖注入还是需要在applicationContext.xml配置bean的。

activemq

spring-boot的pom添加 :

<!--        MQ -->        <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-activemq</artifactId>          </dependency>  

下载activemq,解压,运行bin里的activemq.bat,开启服务,就可以在localhost:8061/admin,用户名密码都是admin访问了。
在topic和queues可以看到发送者和消费者数目,具体内容等消息。其中名字是与代码中

    @JmsListener(destination = "xxx")

对应的,applicationContext.xml配置文件需要配置JMS

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"      xmlns:mongo="http://www.springframework.org/schema/data/mongo"      xsi:schemaLocation="          http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.0.xsd          http://www.springframework.org/schema/data/mongo          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">       <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">     </bean>     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsMessagingTemplate">        <property name="connectionFactory">          <ref bean="connectionFactory"/>        </property>     </bean>     <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">     </bean></beans>  

发送消息:

 @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装      private JmsMessagingTemplate jmsTemplate;      // 发送消息,destination是发送到的队列,message是待发送的消息      public void sendMessage(Destination destination, final String message){          jmsTemplate.convertAndSend(destination, message);      }  

当项目运行后,访问ActiveMQ管理界面就能看到,当前消费着数目为1,如果有其他人也开启这个项目,消费者数会等与总开启项目数,否则为0,这个刷新页面就可以看到更新。
运行代码发送消息后会看到topics和quques数目都相应增加,点击browse可以看到发送的消息内容

原创粉丝点击