場景:javascript
用戶註冊,寫入數據庫成功之後,發送郵件和短信。css
準備工做:html
1)安裝RabbitMQ,參考前面的文章java
2)新建一個名爲RabbitMQAsyncProc的maven web工程,在pom.xml文件裏面引入以下依賴jquery
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.study.demo</groupId> <artifactId>RabbitMQAsyncProc</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>RabbitMQAsyncProc Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.11.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--日誌--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <!-- <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.5</version> </dependency>--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.0.13</version> </dependency> <!--JSON--> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <!-- RabbitMQ --> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>2.0.0.RELEASE</version> </dependency> <!--使用AspectJ方式註解須要相應的包--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.12</version> </dependency> <!--使用AspectJ方式註解須要相應的包--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.12</version> </dependency> </dependencies> <build> <finalName>RabbitMQAsyncProc</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> <resources> <resource> <directory>${basedir}/src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
package com.study.demo.vo; import java.util.UUID; /** * * @Description: 用戶信息實體 * @author liguangsheng * @date 2018年9月18日 * */ public class User { private final String userId; private final String userName; private final String email; private final String phoneNumber; public User(String userId, String userName, String email, String phoneNumber) { this.userId = userId; this.userName = userName; this.email = email; this.phoneNumber = phoneNumber; } public String getUserId() { return userId; } public String getUserName() { return userName; } public String getEmail() { return email; } public String getPhoneNumber() { return phoneNumber; } public static User makeUser(String userName,String email,String phoneNumber){ String userId = UUID.randomUUID().toString(); return new User(userId,userName,email,phoneNumber); } @Override public String toString() { return "User{" + "userId='" + userId + '\'' + ", userName='" + userName + '\'' + ", email='" + email + '\'' + ", phoneNumber='" + phoneNumber + '\'' + '}'; } }
package com.study.demo.service; import com.study.demo.vo.User; /** * * @Description: 用戶註冊接口 * @author leeSmall * @date 2018年9月18日 * */ public interface IUserReg { public boolean userRegister(User user); }
1)保存用戶數據到數據庫git
package com.study.demo.service.busi; import java.util.concurrent.ConcurrentHashMap; import org.springframework.stereotype.Service; import com.study.demo.vo.User; /** * * @Description: 保存用戶數據到數據庫 * @author leeSmall * @date 2018年9月18日 * */ @Service public class SaveUser { private ConcurrentHashMap<String,User> userData = new ConcurrentHashMap<String, User>(); public void saveUser(User user){ try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } userData.putIfAbsent(user.getUserId(),user); } public User getUser(String userId){ return userData.get(userId); } }
2)發送郵件的服務github
package com.study.demo.service.busi; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; /** * * @Description: 發送郵件的服務 * @author leeSmall * @date 2018年9月18日 * */ @Service public class SendEmail { private Logger logger = LoggerFactory.getLogger(SendEmail.class); public void sendEmail(String email){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } logger.info("-------------Already Send email to "+email); } }
3)發送短信的服務web
package com.study.demo.service.busi; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; /** * * @Description: 發送短信的服務 * @author leeSmall * @date 2018年9月18日 * */ @Service public class SendSms { private Logger logger = LoggerFactory.getLogger(SendSms.class); public void sendSms(String phoneNumber){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } logger.info("-------------Already Send Sms to "+phoneNumber); } }
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-2.0.xsd"> <aop:aspectj-autoproxy /> <!-- 配置掃描路徑 --> <context:component-scan base-package="com.study.demo"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- rabbitMQ配置 --> <bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> <constructor-arg value="127.0.0.1"/> <property name="username" value="guest"/> <property name="password" value="guest"/> <property name="channelCacheSize" value="8"/> <property name="port" value="5672"></property> </bean> <rabbit:admin connection-factory="rabbitConnectionFactory"/> <!--郵件相關隊列--> <rabbit:queue name="email_queue" durable="false"/> <!--短信相關隊列--> <rabbit:queue name="sms_queue" durable="false"/> <!--將隊列和交換器經過路由鍵綁定--> <rabbit:direct-exchange name="user-reg-exchange" xmlns="http://www.springframework.org/schema/rabbit" durable="true"> <rabbit:bindings> <rabbit:binding queue="email_queue" key="email" ></rabbit:binding> <rabbit:binding queue="sms_queue" key="sms" ></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- 建立rabbitTemplate 消息模板類 --> <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"> <constructor-arg ref="rabbitConnectionFactory"></constructor-arg> </bean> </beans>
package com.study.demo.service.impl; import com.study.demo.service.IUserReg; import com.study.demo.service.busi.SaveUser; import com.study.demo.service.busi.SendEmail; import com.study.demo.service.busi.SendSms; import com.study.demo.vo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; /** * * @Description: 串行的用戶註冊 * @author leeSmall * @date 2018年9月18日 * */ @Service @Qualifier("serial") public class SerialProcess implements IUserReg { @Autowired private SaveUser saveUser; @Autowired private SendEmail sendEmail; @Autowired private SendSms sendSms; public boolean userRegister(User user) { try { saveUser.saveUser(user); sendEmail.sendEmail(user.getEmail()); sendSms.sendSms(user.getPhoneNumber()); return true; } catch (Exception e) { return false; } } }
package com.study.demo.service.impl; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import com.study.demo.service.IUserReg; import com.study.demo.service.busi.SaveUser; import com.study.demo.service.busi.SendEmail; import com.study.demo.service.busi.SendSms; import com.study.demo.vo.User; /** * * @Description: 並行的用戶註冊 * @author leeSmall * @date 2018年9月18日 * */ @Service @Qualifier("para") public class ParalllerProcess implements IUserReg { private static Logger logger = LoggerFactory.getLogger(ParalllerProcess.class); @Autowired private SaveUser saveUser; @Autowired private SendEmail sendEmail; @Autowired private SendSms sendSms; //發送郵件的線程 private static class SendEmailThread implements Callable<Boolean>{ private SendEmail sendEmail; private String email; public SendEmailThread(SendEmail sendEmail, String email) { this.sendEmail = sendEmail; this.email = email; } public Boolean call() throws Exception { sendEmail.sendEmail(email); logger.info("SendEmailThread send mail to"+email); return true; } } //發送短信的線程 private static class SendSmsThread implements Callable<Boolean>{ private SendSms sendSms; private String phoneNumber; public SendSmsThread(SendSms sendSms, String phoneNumber) { this.sendSms = sendSms; this.phoneNumber = phoneNumber; } public Boolean call() throws Exception { sendSms.sendSms(phoneNumber); logger.info("SendSmsThread send mail to"+phoneNumber); return true; } } public boolean userRegister(User user) { FutureTask<Boolean> sendEmailFuture = new FutureTask<Boolean>(new SendEmailThread(sendEmail,user.getEmail())); FutureTask<Boolean> sendSmsFuture = new FutureTask<Boolean>(new SendSmsThread(sendSms,user.getPhoneNumber())); try { saveUser.saveUser(user); new Thread(sendEmailFuture).start(); new Thread(sendSmsFuture).start(); sendEmailFuture.get();//獲取郵件發送的結果 sendSmsFuture.get();//獲取短信發送的結果 return true; } catch (Exception e) { logger.error(e.toString()); return false; } } }
package com.study.demo.service.impl; import com.study.demo.service.IUserReg; import com.study.demo.service.busi.SaveUser; import com.study.demo.vo.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.AmqpException; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; /** * * @Description: RabbitMQ實現的異步用戶註冊 * @author leeSmall * @date 2018年9月18日 * */ @Service @Qualifier("async") public class AsyncProcess implements IUserReg{ private Logger logger = LoggerFactory.getLogger(AsyncProcess.class); @Autowired private RabbitTemplate rabbitTemplate; @Autowired private SaveUser saveUser; public boolean userRegister(User user) { try { saveUser.saveUser(user); rabbitTemplate.send("user-reg-exchange","email", new Message(user.getEmail().getBytes(),new MessageProperties())); rabbitTemplate.send("user-reg-exchange","sms", new Message(user.getEmail().getBytes(),new MessageProperties())); } catch (AmqpException e) { logger.error(e.toString()); return false; } return true; } }
package com.study.demo.service.mq; import com.study.demo.service.busi.SendEmail; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * * @Description: RabbitMQ消息消費端監聽郵件消息類 * @author leeSmall * @date 2018年9月18日 * */ @Component public class ProcessUserEmail implements MessageListener { private Logger logger = LoggerFactory.getLogger(ProcessUserEmail.class); @Autowired private SendEmail sendEmail; public void onMessage(Message message) { logger.info("accept message,ready process......"); sendEmail.sendEmail(new String(message.getBody())); } }
在/RabbitMQAsyncProc/src/main/java/applicationContext.xml添加郵件消息監聽類配置:ajax
<rabbit:listener-container connection-factory="rabbitConnectionFactory"> <rabbit:listener queues="email_queue" ref="processUserEmail" method="onMessage"/> </rabbit:listener-container>
package com.study.demo.service.mq; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.study.demo.service.busi.SendSms; /** * * @Description: RabbitMQ消息消費端監聽sms消息類 * @author leeSmall * @date 2018年9月18日 * */ @Component public class ProcessUserSms implements MessageListener { private Logger logger = LoggerFactory.getLogger(ProcessUserSms.class); @Autowired private SendSms sendSms; public void onMessage(Message message) { logger.info("accept message,ready process......"); sendSms.sendSms(new String(message.getBody())); } }
在/RabbitMQAsyncProc/src/main/java/applicationContext.xml添加短信消息監聽類配置:spring
<rabbit:listener-container connection-factory="rabbitConnectionFactory"> <rabbit:listener queues="email_queue" ref="processUserEmail" method="onMessage"/> <rabbit:listener queues="sms_queue" ref="processUserSms" method="onMessage"/> </rabbit:listener-container>
package com.study.demo.tools; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import com.study.demo.vo.User; /** * * @Description: 統計花費時間工具類 * @author leeSmall * @date 2018年9月18日 * */ @Aspect @Service public class StatTime { private Logger logger = LoggerFactory.getLogger(StatTime.class); private User user; public StatTime() { logger.info("************Aop開啓"); } @Pointcut("execution(* com.study.demo.service.impl.*.*Register(..))") public void stat(){} @Around("stat()&&args(user)") public Object statTime(ProceedingJoinPoint proceedingJoinPoint,User user){ this.user = user; long start = System.currentTimeMillis(); Object result = null; try { result = proceedingJoinPoint.proceed(new Object[]{this.user}); } catch (Throwable throwable) { throwable.printStackTrace(); } logger.info("************spend time : "+(System.currentTimeMillis()-start)+"ms"); return result; } }
package com.study.demo.controller; import com.study.demo.service.IUserReg; import com.study.demo.vo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; /** * * @Description: 用戶註冊控制器 * @author leeSmall * @date 2018年9月18日 * */ @Controller public class UserRegController { private static final String SUCCESS = "suc"; private static final String FAILUER = "failure"; @Autowired @Qualifier("serial") private IUserReg userReg; @RequestMapping("/userReg") public String userReg(){ return "index"; } @RequestMapping("/saveUser") @ResponseBody public String saveUser(@RequestParam("userName")String userName, @RequestParam("email")String email, @RequestParam("phoneNumber")String phoneNumber){ try { if (userReg.userRegister(User.makeUser(userName,email,phoneNumber))) return SUCCESS; else return FAILUER; } catch (Exception e) { return FAILUER; } } }
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- <mvc:default-servlet-handler />--> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> <context:component-scan base-package="com.study.demo.controller"> <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />--> </context:component-scan> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <bean class="org.springframework.http.MediaType"> <constructor-arg index="0" value="text" /> <constructor-arg index="1" value="plain" /> <constructor-arg index="2" value="UTF-8" /> </bean> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter" /> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="mediaTypes"> <map> <entry key="html" value="text/html" /> <entry key="pdf" value="application/pdf" /> <entry key="xsl" value="application/vnd.ms-excel" /> <entry key="xml" value="application/xml" /> <entry key="json" value="application/json" /> </map> </property> <property name="defaultContentType" value="text/html" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="0" /> <property name="contentNegotiationManager" ref="contentNegotiationManager" /> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp"></property> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> <property name="extractValueFromSingleKeyModel" value="true" /> </bean> </list> </property> </bean> </beans>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); System.out.println(path); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; System.out.println(basePath); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>用戶註冊-異步模式</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <script type="text/javascript" src="<%--<%=basePath%>--%>resources/js/jquery-1.11.0.min.js"></script> <style type="text/css"> .h1 { margin: 0 auto; } #producer{ width: 48%; border: 1px solid blue; height: 80%; align:center; margin:0 auto; } body{ text-align :center; } div { text-align :center; } textarea{ width:80%; height:100px; border:1px solid gray; } button{ background-color: rgb(62, 156, 66); border: none; font-weight: bold; color: white; height:30px; } </style> <script type="text/javascript"> function send(){ $.ajax({ type: 'get', url:'<%=basePath%>saveUser?userName='+$("#userName").val() +'&email='+$("#userEmail").val()+'&phoneNumber='+$("#userNumber").val(), dataType:'text', success:function(data){ if(data=="suc"){ alert("註冊成功!"); }else{ alert("註冊失敗!"); } }, error:function(data){ alert("註冊錯誤!"); } }); } </script> </head> <body> <h1>用戶註冊-異步模式</h1> <div id="producer"> 用戶姓名:<input type="text" id="userName"/> <br> 用戶郵件:<input type="text" id="userEmail"/> <br> 用戶手機:<input type="text" id="userNumber"/> <br> <button onclick="send()">注 冊</button> <br> </div> </body> </html>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>RabbitMqSpringProducerDemo</display-name> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <!-- Spring 編碼過濾器 start --> <filter> <filter-name>characterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring 編碼過濾器 End --> <!-- Spring Application Context Listener Start --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring Application Context Listener End --> <!-- Spring MVC Config Start --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- Filter all resources --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring MVC Config End --> </web-app>
到此代碼別寫完成!
首次啓動時用戶註冊接口時串行的
@Autowired @Qualifier("serial") private IUserReg userReg;
在瀏覽器輸入地址http://localhost:8080/RabbitMQAsyncProc/userReg訪問進行用戶註冊
查看控制檯狀態:
修改用戶註冊接口爲並行的:
@Autowired @Qualifier("para") private IUserReg userReg;
在用戶界面註冊 查看控制檯狀態:
修改用戶註冊接口爲RabbitMQ異步實現的的:
@Autowired @Qualifier("async") private IUserReg userReg;
在用戶界面註冊 查看控制檯狀態:
總結:
串行模式 ************spend time : 251ms
並行模式 ************spend time : 153ms
消息隊列模式:************spend time : 59ms
因此RabbitMQ異步處理的性能最快
場景:
用戶下訂單買商品,訂單成功了,去扣減庫存,庫存必須扣減完成,沒有庫存,庫存低於某個閾值,能夠扣減成功,要通知其餘系統(如採購系統儘快採購,用戶訂單系統咱們儘快調貨)
RPC實現。庫存系統失敗,訂單系統也沒法成功,訂單系統和庫存系統耦合了。因此要緩存消息中間件來解耦。發送一個扣減庫存的消息,保證消息必須被庫存系統處理。
三個問題要解決:
1)訂單系統發給MQ服務器的消息,必須被MO服務器接收到(事物、發送者確認)
2)MQ服務器拿到消息之後,消息被正常處理之前必須保存住(持久化)
3)某個庫存服務出現了異常,消息要可以被其餘庫存系統處理(消費者確認,消息監聽類要實現ChannelAwareMessageListener)
訂單系統必定要知道庫存系統是否處理成功怎麼辦?
庫存系統和訂單系統之間創建一個消息通道,庫存系統去通知訂單系統
準備工做:
1)安裝RabbitMQ,參考前面的文章
2)分別新建名爲RabbitMQOrder和RabbitMQDepot的兩個maven web工程,在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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.study.demo</groupId> <artifactId>RabbitMQOrder</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>RabbitMQOrder Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.11.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--日誌 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.0.13</version> </dependency> <!--JSON --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency> <!-- RabbitMQ --> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>2.0.0.RELEASE</version> </dependency> <!--使用AspectJ方式註解須要相應的包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.12</version> </dependency> <!--使用AspectJ方式註解須要相應的包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.12</version> </dependency> </dependencies> <build> <finalName>RabbitMQOrder</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> <resources> <resource> <directory>${basedir}/src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
package com.study.demo.vo; import java.io.Serializable; /** * * @Description: 商品實體 * @author leeSmall * @date 2018年9月19日 * */ public class GoodTransferVo implements Serializable { private static final long serialVersionUID = 7702481109435751937L; /** * 商品id */ private String goodsId; /** * 改變的庫存量 */ private int changeAmount; /** * 入庫或者出庫 */ private boolean inOrOut; public String getGoodsId() { return goodsId; } public void setGoodsId(String goodsId) { this.goodsId = goodsId; } public int getChangeAmount() { return changeAmount; } public void setChangeAmount(int changeAmount) { this.changeAmount = changeAmount; } public boolean isInOrOut() { return inOrOut; } public void setInOrOut(boolean inOrOut) { this.inOrOut = inOrOut; } }
package com.study.demo.service; /** * * @Description: 處理庫存接口 * @author leeSmall * @date 2018年9月19日 * */ public interface IProDepot { public void processDepot(String goodsId,int amount); }
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-2.0.xsd"> <!-- 配置掃描路徑 --> <context:component-scan base-package="com.study.demo"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- rabbitMQ配置 --> <bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> <constructor-arg value="127.0.0.1"/> <property name="username" value="guest"/> <property name="password" value="guest"/> <property name="channelCacheSize" value="8"/> <property name="port" value="5672"></property> </bean> <rabbit:admin connection-factory="rabbitConnectionFactory"/> <rabbit:queue name="depot_queue" durable="true"/> <!--隊列經過路由鍵和交換器綁定 --> <rabbit:direct-exchange name="depot-amount-exchange" xmlns="http://www.springframework.org/schema/rabbit" durable="true"> <rabbit:bindings> <rabbit:binding queue="depot_queue" key="amount.depot" ></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- 建立rabbitTemplate 消息模板類 --> <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"> <constructor-arg ref="rabbitConnectionFactory"></constructor-arg> </bean> </beans>
package com.study.demo.service; import com.study.demo.vo.GoodTransferVo; import com.google.gson.Gson; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageDeliveryMode; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; /** * * @Description: RabbitMQ處理庫存接口的實現類 * @author leeSmall * @date 2018年9月19日 * */ @Service @Qualifier("mq") public class MqMode implements IProDepot { private final static String DEPOT_RK = "amount.depot"; private final static String DEPOT_EXCHANGE = "depot-amount-exchange"; @Autowired RabbitTemplate rabbitTemplate; private static Gson gson = new Gson(); public void processDepot(String goodsId, int amount) { GoodTransferVo goodTransferVo = new GoodTransferVo(); goodTransferVo.setGoodsId(goodsId); goodTransferVo.setChangeAmount(amount); goodTransferVo.setInOrOut(false); String goods = gson.toJson(goodTransferVo); MessageProperties messageProperties = new MessageProperties(); messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT); rabbitTemplate.send(DEPOT_EXCHANGE, DEPOT_RK, new Message(goods.getBytes(), messageProperties)); } }
package com.study.demo.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; /** * * @Description: 處理訂單業務類 * @author leeSmall * @date 2018年9月19日 * */ @Service public class ProcessOrder { private Logger logger = LoggerFactory.getLogger(ProcessOrder.class); @Autowired @Qualifier("mq") private IProDepot proDepot; public void processOrder(String goodsId,int amount){ try { Thread.sleep(80); } catch (InterruptedException e) { e.printStackTrace(); } logger.info("--------------------["+goodsId+"]訂單入庫完成,準備變更庫存!"); proDepot.processDepot(goodsId,amount); } }
package com.study.demo.service.callback; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.support.CorrelationData; import org.springframework.stereotype.Service; /** * * @Description: RabbitMQ發送者確認 * @author leeSmall * @date 2018年9月19日 * */ @Service public class ConfirmCallback implements RabbitTemplate.ConfirmCallback { private Logger logger = LoggerFactory.getLogger(ConfirmCallback.class); public void confirm(CorrelationData correlationData, boolean ack, String cause) { if (ack) { logger.info("消息確認發送給mq成功"); } else { //處理失敗的消息 logger.info("消息發送給mq失敗,考慮重發:"+cause); } } }
package com.study.demo.service.callback; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Service; /** * * @Description: RabbitMQ返回確認-RabbitMQ服務內部出錯時回調 * @author leeSmall * @date 2018年9月19日 * */ @Service public class SendReturnCallback implements RabbitTemplate.ReturnCallback { private Logger logger = LoggerFactory.getLogger(SendReturnCallback.class); public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) { logger.info("Returned replyText:"+replyText); logger.info("Returned exchange:"+exchange); logger.info("Returned routingKey:"+routingKey); String msgJson = new String(message.getBody()); logger.info("Returned Message:"+msgJson); } }
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-2.0.xsd"> <!-- 配置掃描路徑 --> <context:component-scan base-package="com.study.demo"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- rabbitMQ配置 --> <bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> <constructor-arg value="127.0.0.1"/> <property name="username" value="guest"/> <property name="password" value="guest"/> <property name="channelCacheSize" value="8"/> <property name="port" value="5672"></property> <!-- 發佈確認必須配置在CachingConnectionFactory上 --> <property name="publisherConfirms" value="true"/> </bean> <rabbit:admin connection-factory="rabbitConnectionFactory"/> <rabbit:queue name="depot_queue" durable="true"/> <!--隊列經過路由鍵和交換器綁定 --> <rabbit:direct-exchange name="depot-amount-exchange" xmlns="http://www.springframework.org/schema/rabbit" durable="true"> <rabbit:bindings> <rabbit:binding queue="depot_queue" key="amount.depot" ></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- 建立rabbitTemplate 消息模板類 --> <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"> <constructor-arg ref="rabbitConnectionFactory"></constructor-arg> <!--消息確認回調 --> <property name="confirmCallback" ref="confirmCallback"/> <property name="returnCallback" ref="sendReturnCallback"/> </bean> </beans>
package com.study.demo.controller; import com.study.demo.service.ProcessOrder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; /** * * @Description: 訂單業務控制器 * @author leeSmall * @date 2018年9月19日 * */ @Controller public class OrderController { private Logger logger = LoggerFactory.getLogger(OrderController.class); private static final String SUCCESS = "suc"; private static final String FAILUER = "failure"; @Autowired private ProcessOrder processOrder; @RequestMapping("/order") public String order(){ return "index"; } @RequestMapping("/confirmOrder") @ResponseBody public String confirmOrder(@RequestParam("goodsId")String goodsId, @RequestParam("amount")int amount){ try { processOrder.processOrder(goodsId,amount); return SUCCESS; } catch (Exception e) { logger.error("訂單確認異常!",e); return FAILUER; } } }
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- <mvc:default-servlet-handler />--> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> <context:component-scan base-package="com.study.demo.controller"> <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />--> </context:component-scan> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <bean class="org.springframework.http.MediaType"> <constructor-arg index="0" value="text" /> <constructor-arg index="1" value="plain" /> <constructor-arg index="2" value="UTF-8" /> </bean> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter" /> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="mediaTypes"> <map> <entry key="html" value="text/html" /> <entry key="pdf" value="application/pdf" /> <entry key="xsl" value="application/vnd.ms-excel" /> <entry key="xml" value="application/xml" /> <entry key="json" value="application/json" /> </map> </property> <property name="defaultContentType" value="text/html" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="0" /> <property name="contentNegotiationManager" ref="contentNegotiationManager" /> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp"></property> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> <property name="extractValueFromSingleKeyModel" value="true" /> </bean> </list> </property> </bean> </beans>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); System.out.println(path); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; System.out.println(basePath); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>訂單提交</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <script type="text/javascript" src="<%--<%=basePath%>--%>resources/js/jquery-1.11.0.min.js"></script> <style type="text/css"> .h1 { margin: 0 auto; } #producer{ width: 48%; border: 1px solid blue; height: 80%; align:center; margin:0 auto; } body{ text-align :center; } div { text-align :center; } textarea{ width:80%; height:100px; border:1px solid gray; } button{ background-color: rgb(62, 156, 66); border: none; font-weight: bold; color: white; height:30px; } </style> <script type="text/javascript"> function send(){ $.ajax({ type: 'get', url:'<%=basePath%>confirmOrder?goodsId='+$("#goods").val() +'&amount='+$("#amount").val(), dataType:'text', success:function(data){ if(data=="suc"){ alert("提交成功!"); }else{ alert("提交失敗!"); } }, error:function(data){ alert("提交錯誤!"); } }); } </script> </head> <body> <h1>確認提交訂單</h1> <div id="producer"> 商品編號:<input type="text" id="goods"/> <br> 訂單數量:<input type="text" id="amount"/> <br> <button onclick="send()">確 認</button> <br> </div> </body> </html>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>RabbitMQOrder</display-name> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <!-- Spring 編碼過濾器 start --> <filter> <filter-name>characterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring 編碼過濾器 End --> <!-- Spring Application Context Listener Start --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring Application Context Listener End --> <!-- Spring MVC Config Start --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- Filter all resources --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring MVC Config End --> </web-app>
package com.study.demo.vo; import java.io.Serializable; /** * * @Description: 商品實體 * @author leeSmall * @date 2018年9月19日 * */ public class GoodTransferVo implements Serializable { private static final long serialVersionUID = 7702481109435751937L; /** * 商品id */ private String goodsId; /** * 改變的庫存量 */ private int changeAmount; /** * 入庫或者出庫 */ private boolean inOrOut; public String getGoodsId() { return goodsId; } public void setGoodsId(String goodsId) { this.goodsId = goodsId; } public int getChangeAmount() { return changeAmount; } public void setChangeAmount(int changeAmount) { this.changeAmount = changeAmount; } public boolean isInOrOut() { return inOrOut; } public void setInOrOut(boolean inOrOut) { this.inOrOut = inOrOut; } }
package com.study.demo.service; import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiFunction; import javax.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; /** * * @Description: 庫存數據服務 * @author leeSmall * @date 2018年9月19日 * */ @Service public class Depot { private static Logger logger = LoggerFactory.getLogger(Depot.class); private ConcurrentHashMap<String,Integer> goodsData = new ConcurrentHashMap<String, Integer>(); @PostConstruct public void initDepot(){ goodsData.put("001",1000); goodsData.put("002",500); goodsData.put("003",600); goodsData.put("004",700); } //增長庫存 public void inDepot(String goodsId,int addAmout){ logger.info("+++++++++++++++++增長商品:"+goodsId+"庫存,數量爲:"+addAmout); int newValue = goodsData.compute(goodsId, new BiFunction<String, Integer, Integer>() { public Integer apply(String s, Integer integer) { return integer == null ? addAmout : integer + addAmout; } }); logger.info("+++++++++++++++++商品:"+goodsId+"庫存,數量變爲:"+newValue); } //減小庫存 public void outDepot(String goodsId,int reduceAmout){ logger.info("-------------------減小商品:"+goodsId+"庫存,數量爲:"+reduceAmout); int newValue = goodsData.compute(goodsId, new BiFunction<String, Integer, Integer>() { public Integer apply(String s, Integer integer) { return integer == null ? 0 : integer - reduceAmout; } }); logger.info("-------------------商品:"+goodsId+"庫存,數量變爲:"+newValue); } public int getGoodsAmount(String goodsId){ return goodsData.get(goodsId); } }
package com.study.demo.service; import com.study.demo.vo.GoodTransferVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * * @Description: 庫存服務管理 * @author leeSmall * @date 2018年9月19日 * */ @Service public class DepotManager { @Autowired private Depot depot; public void operDepot(GoodTransferVo goodTransferVo){ if(goodTransferVo.isInOrOut()){ depot.inDepot(goodTransferVo.getGoodsId(),goodTransferVo.getChangeAmount()); }else{ depot.outDepot(goodTransferVo.getGoodsId(),goodTransferVo.getChangeAmount()); } } }
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-2.0.xsd"> <!-- 配置掃描路徑 --> <context:component-scan base-package="com.study.demo"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- rabbitMQ配置 --> <bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> <constructor-arg value="127.0.0.1"/> <property name="username" value="guest"/> <property name="password" value="guest"/> <property name="channelCacheSize" value="8"/> <property name="port" value="5672"></property> </bean> <rabbit:admin connection-factory="rabbitConnectionFactory"/> <!--配置隊列 --> <rabbit:queue name="depot_queue" durable="true"/> <!--隊列和交換器綁定 --> <rabbit:direct-exchange name="depot-amount-exchange" xmlns="http://www.springframework.org/schema/rabbit" durable="true"> <rabbit:bindings> <rabbit:binding queue="depot_queue" key="amount.depot" ></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- 對消息要手動確認 --> <rabbit:listener-container connection-factory="rabbitConnectionFactory" acknowledge="manual"> </rabbit:listener-container> </beans>
package com.study.demo.mq; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.google.gson.Gson; import com.rabbitmq.client.Channel; import com.study.demo.service.DepotManager; import com.study.demo.vo.GoodTransferVo; /** * * @Description: 消息機制處理庫存 * @author leeSmall * @date 2018年9月19日 * */ @Service public class ProcessDepot implements ChannelAwareMessageListener { private static Logger logger = LoggerFactory.getLogger(ProcessDepot.class); @Autowired private DepotManager depotManager; private static Gson gson = new Gson(); @Override public void onMessage(Message message, Channel channel) throws Exception { try { String msg = new String(message.getBody()); logger.info(">>>>>>>>>>>>>>接收到消息:"+msg); GoodTransferVo goodTransferVo = gson.fromJson(msg,GoodTransferVo.class); try { depotManager.operDepot(goodTransferVo); channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); logger.info(">>>>>>>>>>>>>>庫存處理完成,應答Mq服務"); } catch (Exception e) { logger.error(e.getMessage()); channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,true); logger.info(">>>>>>>>>>>>>>庫存處理失敗,拒絕消息,要求Mq從新派發"); throw e; } } catch (Exception e) { logger.error(e.getMessage()); } } }
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-2.0.xsd"> <!-- 配置掃描路徑 --> <context:component-scan base-package="com.study.demo"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- rabbitMQ配置 --> <bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> <constructor-arg value="127.0.0.1"/> <property name="username" value="guest"/> <property name="password" value="guest"/> <property name="channelCacheSize" value="8"/> <property name="port" value="5672"></property> </bean> <rabbit:admin connection-factory="rabbitConnectionFactory"/> <!--配置隊列 --> <rabbit:queue name="depot_queue" durable="true"/> <!--隊列和交換器綁定 --> <rabbit:direct-exchange name="depot-amount-exchange" xmlns="http://www.springframework.org/schema/rabbit" durable="true"> <rabbit:bindings> <rabbit:binding queue="depot_queue" key="amount.depot" ></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- 對消息要手動確認 --> <rabbit:listener-container connection-factory="rabbitConnectionFactory" acknowledge="manual"> <rabbit:listener queues="depot_queue" ref="processDepot" method="onMessage" /> </rabbit:listener-container> </beans>
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- <mvc:default-servlet-handler />--> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> <context:component-scan base-package="com.study.demo.controller"> <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />--> </context:component-scan> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <bean class="org.springframework.http.MediaType"> <constructor-arg index="0" value="text" /> <constructor-arg index="1" value="plain" /> <constructor-arg index="2" value="UTF-8" /> </bean> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter" /> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="mediaTypes"> <map> <entry key="html" value="text/html" /> <entry key="pdf" value="application/pdf" /> <entry key="xsl" value="application/vnd.ms-excel" /> <entry key="xml" value="application/xml" /> <entry key="json" value="application/json" /> </map> </property> <property name="defaultContentType" value="text/html" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="0" /> <property name="contentNegotiationManager" ref="contentNegotiationManager" /> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp"></property> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> <property name="extractValueFromSingleKeyModel" value="true" /> </bean> </list> </property> </bean> </beans>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>RabbitMQDepot</display-name> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <!-- Spring 編碼過濾器 start --> <filter> <filter-name>characterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring 編碼過濾器 End --> <!-- Spring Application Context Listener Start --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring Application Context Listener End --> <!-- Spring MVC Config Start --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- Filter all resources --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring MVC Config End --> </web-app>
庫存系統狀態:
訂單系統狀態: