hjr-JAVA:Spring-boot和MQ-ActiveMQ +JMS

#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可以看到发送的消息内容

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

架构师小侯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值