搜索词>>activemq 耗时0.0020
  • spring boot整合activemq实现MQ的使用-java编程中

    spring boot整合activemq。本博客将通过一个简单的例子讲解在spring boot中activemq如何作为消费者(Consumer )和如何在spring boot中消息提供者(Provider )的实现。<h2>一.去activemq官网下载mq软件</h2> 去Apache官网下载activemq软件,并安装。 <h2>二.编写Java代码</h2> java编程中spring boot整合activemq实现MQ的使用<br /> 1.在spring boot项目中添加activemq的依赖 <pre> <code class="language-xml"><!-- activemq support --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <!-- /activemq support --></code></pre> 2.application.properties配置文件中添加mq的相关配置 <pre> <code>#==================activemq Config Start================== spring.activemq.broker-url=tcp://127.0.0.1:61616 spring.activemq.in-memory=true spring.activemq.password=admin spring.activemq.user=admin spring.activemq.packages.trust-all=false spring.activemq.packages.trusted= spring.activemq.pool.configuration.*= spring.activemq.pool.enabled=false spring.activemq.pool.expiry-timeout=0 spring.activemq.pool.idle-timeout=30000 spring.activemq.pool.max-connections=1 #==================activemq Config End ==================</code></pre> <br /> 3.消息提供者 <pre> <code class="language-java">package com.htwl.collection.cqyth.amq; import java.text.SimpleDateFormat; import java.util.Date; import javax.jms.Destination; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * MQ消息提供者 * * @author leftso * */ @Component @EnableScheduling // 测试用启用任务调度 public class Provider { /** MQ jms实例 **/ @Autowired private JmsMessagingTemplate jmsMessagingTemplate; private static int count = 0; @Scheduled(fixedDelay = 5000) // 每5s执行1次-测试使用 public void send() { Destination destination = new ActiveMQQueue("TEST_QUEUE_LOG");// 这里定义了Queue的key String message = "Send AMQ Test ..." + count; System.out.println("[" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "]" + message); count++; this.jmsMessagingTemplate.convertAndSend(destination, message); } } </code></pre> 4.消费 <pre> <code class="language-java">package com.htwl.collection.cqyth.amq; import java.text.SimpleDateFormat; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; @Component public class Consumer { Logger LOGGER = LoggerFactory.getLogger(this.getClass()); /** * 使用@JmsListener注解来监听指定的某个队列内的消息 **/ @JmsListener(destination = "TEST_QUEUE_LOG") public void removeMessage(String msg) { System.out.println("["+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())+"]Receive:"+msg); } } </code></pre>  
  • Java编程之Spring Boot通过JMSTemplate 整合ActiveMQ

    Java编程之Spring Boot通过JMSTemplate 整合ActiveMQ<h2>一.项目结构<br /> 本教程创建的应用程序结构如下:</h2> <strong><img alt="Spring-Boot-Jersey-Project-Structure" class="img-thumbnail" src="/resources/assist/images/blog/034cb03ceabf43fea761e136b7e97a9b.png" /></strong> <h2>二.从Spring Initializr创建Spring boot应用程序</h2> <strong>1.访问Spring Initializr网站,并通过 Jersey (JAX-RS)依赖创建spring boot项目</strong><br /> <img alt="创建spring boot项目" class="img-thumbnail" src="/resources/assist/images/blog/9eee2ca48f664f0e8949e824feb21a90.png" /><br /> <strong>2.创建zip文件的项目。解压到自己电脑的某个地方。通过eclipse中的导入maven 已经存在的项目导入该项目</strong><br /> <strong>3.检查项目的maven依赖文件,看是否有加入spring-boot-starter-jersey的依赖。</strong> <pre> <code class="language-xml"><dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies></code></pre> <h2>四.创建JAX-RS REST资源文件</h2> 现在创建一些JAX-RS资源用来做测试。我这里创建了一个UserResource class.<br /> <strong>UserResource.java:</strong> <pre> <code class="language-java">package com.howtodoinjava.jerseydemo; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; @XmlAccessorType(XmlAccessType.NONE) @XmlRootElement(name = "users") @Path("/users") public class UserResource { private static Map<Integer, User> DB = new HashMap<>(); @GET @Produces("application/json") public Users getAllUsers() { Users users = new Users(); users.setUsers(new ArrayList<>(DB.values())); return users; } @POST @Consumes("application/json") public Response createUser(User user) throws URISyntaxException { if(user.getFirstName() == null || user.getLastName() == null) { return Response.status(400).entity("Please provide all mandatory inputs").build(); } user.setId(DB.values().size()+1); user.setUri("/user-management/"+user.getId()); DB.put(user.getId(), user); return Response.status(201).contentLocation(new URI(user.getUri())).build(); } @GET @Path("/{id}") @Produces("application/json") public Response getUserById(@PathParam("id") int id) throws URISyntaxException { User user = DB.get(id); if(user == null) { return Response.status(404).build(); } return Response .status(200) .entity(user) .contentLocation(new URI("/user-management/"+id)).build(); } @PUT @Path("/{id}") @Consumes("application/json") @Produces("application/json") public Response updateUser(@PathParam("id") int id, User user) throws URISyntaxException { User temp = DB.get(id); if(user == null) { return Response.status(404).build(); } temp.setFirstName(user.getFirstName()); temp.setLastName(user.getLastName()); DB.put(temp.getId(), temp); return Response.status(200).entity(temp).build(); } @DELETE @Path("/{id}") public Response deleteUser(@PathParam("id") int id) throws URISyntaxException { User user = DB.get(id); if(user != null) { DB.remove(user.getId()); return Response.status(200).build(); } return Response.status(404).build(); } static { User user1 = new User(); user1.setId(1); user1.setFirstName("John"); user1.setLastName("Wick"); user1.setUri("/user-management/1"); User user2 = new User(); user2.setId(2); user2.setFirstName("Harry"); user2.setLastName("Potter"); user2.setUri("/user-management/2"); DB.put(user1.getId(), user1); DB.put(user2.getId(), user2); } }</code></pre> <strong>Users.java</strong> <pre> <code class="language-java">package com.howtodoinjava.jerseydemo; import java.util.ArrayList; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlAccessorType(XmlAccessType.NONE) @XmlRootElement(name = "users") public class Users { @XmlElement(name="user") private ArrayList<User> users; public ArrayList<User> getUsers() { return users; } public void setUsers(ArrayList<User> users) { this.users = users; } }</code></pre> <strong>User.java</strong> <pre> <code class="language-java">package com.howtodoinjava.jerseydemo; import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlAccessorType(XmlAccessType.NONE) @XmlRootElement(name = "user") public class User implements Serializable { private static final long serialVersionUID = 1L; @XmlAttribute(name = "id") private int id; @XmlAttribute(name="uri") private String uri; @XmlElement(name = "firstName") private String firstName; @XmlElement(name = "lastName") private String lastName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getUri() { return uri; } public void setUri(String uri) { this.uri = uri; } }</code></pre> <h2>五.Jersey配置</h2> <br /> <strong>1.现在我们有一个jax - rs资源,我们想从spring boot应用程序中访问它,其中包括Jersey依赖。让我们将此资源注册为Jersey资源。</strong> <pre> <code class="language-java">package com.howtodoinjava.jerseydemo; import org.glassfish.jersey.server.ResourceConfig; import org.springframework.stereotype.Component; @Component public class JerseyConfig extends ResourceConfig { public JerseyConfig() { register(UserResource.class); } }</code></pre> 查看@ component注释。它允许在spring引导自动扫描源文件夹中的java类时注册这个类。<br /> <br /> <strong>2.ResourceConfig提供了简化jax - rs组件注册的高级功能。</strong><br /> <br /> <strong>3.通过<code>SpringBootServletInitializer扩展spring boot项目</code></strong> <pre> <code class="language-java">package com.howtodoinjava.jerseydemo; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication public class JerseydemoApplication extends SpringBootServletInitializer { public static void main(String[] args) { new JerseydemoApplication().configure(new SpringApplicationBuilder(JerseydemoApplication.class)).run(args); } }</code></pre> <h2>六.demo</h2> 启动spring boot项目,并测试资源<br /> <strong>Access /users resource<br /> <img alt="Access /users resource" class="img-thumbnail" src="/resources/assist/images/blog/fdc261ca174b41cf88e9884c46db3762.png" /><br /> Access /users/1 resource<br /> <img alt="Access /users/1 resource" class="img-thumbnail" src="/resources/assist/images/blog/0fb858b938bf41129a695db1e9d85a68.png" /></strong><br />