SPRING-AMQP Integrated RABBITMQ consumer configuration and code

Source: Internet
Author: User
Tags rabbitmq

<?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:rabbit= "Http://www.springframework.org/schema/rabbit"         xsi:schemalocation= "             http://www.springframework.org/schema/beans                 http://www.springframework.org/schema/beans/spring-beans.xsd             http://www.springframework.org/schema/ context                http:// Www.springframework.org/schema/context/spring-context.xsd            http:// www.springframework.org/schema/rabbit                 http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd ">          <!--  Connection service configuration   -->   <rabbit: Connection-factory id= "mqconnectionfactory"  host= "localhost"  username= "Guest"          password= "Guest"  port= "5672"   />             <rabbit:admin connection-factory= "MqConnectionFactory"/>        <!-- queue  Queue Declaration-->   <rabbit:queue  id= "Queue_one"  durable= "true"  auto-delete= "false"  exclusive= "false"  name= "Queue_one" />        <!--  Configuring thread pool  -->  <bean id = " Taskexecutor "  class =" Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor "  >      <!--  Thread pool maintain minimum number of threads  -->  <property  name = "Corepoolsize"  value = "5"  />      <!--  The thread pool maintains idle time allowed by threads  -->  <property name = "Keepaliveseconds"  value = "30000 The  />      <!--  thread pool maintains the maximum number of threads  -->  <property  name = "Maxpoolsize"  value = " />      <!-- Buffer queue used by the   thread pool  -->  <property name = "queuecapacity"  value = "200"  />  </bean>     <!-- exchange queue  binging key  binding &NBSP;--&GT;&NBSp;   <rabbit:direct-exchange name= "My-mq-exchange"  durable= "true"   Auto-delete= "false"  id= "My-mq-exchange" >        <rabbit: bindings>            <rabbit:binding  Queue= "Queue_one"  key= "Queue_one_key"/>        </rabbit: bindings>    </rabbit:direct-exchange>        <!--  Message listener  -->      <bean id= "Consumermessagelistener"  class= "Com.netease.mobileMq.task.deviceCacheFlushTask"/>      <!--   can get messagelistener -->      <!--of Session  queue  litener   observation   Monitoring mode   notifies listeners on the corresponding queue when a message arrives-->    <rabbit: Listener-container connection-factory= "MQConnectionFactory " acknowledge=" Auto " task-executor=" Taskexecutor ">         <rabbit:listener queues= "Queue_one"  ref= "Consumermessagelistener"/>     </rabbit:listener-container></beans>
package com.netease.mobilemq.task;import java.util.date;import java.util.list;import  org.springframework.amqp.core.amqptemplate;import org.springframework.amqp.core.message;import  org.springframework.amqp.core.messagelistener;import org.apache.commons.lang3.stringutils;import  org.slf4j.logger;import org.slf4j.loggerfactory;import  org.springframework.beans.factory.annotation.autowired;import org.springframework.jms.core.jmstemplate; import com.alibaba.fastjson.json;import com.netease.commonbean.flushdevicecachebean;import  com.netease.device.constant.equipmentconstants;import com.netease.device.dao.equipmentmapper;import  com.netease.device.dao.fingerusermapper;import com.netease.device.entity.equipmentinfo;import  com.netease.device.entity.fingeruserinfo;import com.netease.mobile.common.redisutil;/** * @ author  author  E-mail:[email protected] *  @version   creation time: 2015 Year August 4   PM 4:44:39 *  class Description  */public class deviceCacheFlushTask implements   messagelistener{private static logger logger = loggerfactory.getlogger (" Equipmenterrorlog "), @AutowiredFingerUserMapper  fingerUserMapper; @AutowiredEquipmentMapper   Equipmentmapper, @Overridepublic  void onmessage (message  message) {  string  Receivemsg=null;try {receivemsg =new string (Message.getbody (), "Utf-8");}  catch  (EXCEPTION&NBSP;E1)  {// TODO Auto-generated catch  Blocke1.printstacktrace (); return ;} System.out.println ("receivemsg:" +receivemsg);if  (Stringutils.isblank (receivemsg))  {logger.error (" devicecacheflushtask   receivemsg is null  time is  " +  New date ()); return ;} Else {logger.info ("devicecacheflushtask   receivemsg " &NBSP;+&NBSP;RECEIVEMSG);// Recorded in logEach refreshed data}flushdevicecachebean flushbean = json.parseobject (receivemsg,  Flushdevicecachebean.class);//Format the flushed object that is passed over. STRING&NBSP;MAINSSN&NBSP;=&NBSP;FLUSHBEAN.GETMAINSSN (); String[] refids = flushbean.getuserids ()/*---------------First update the user cache----------*/if  (mainssn  != null)  {//have master account to update master account information List<fingeruserinfo> fingeruserinfos = null;try  {fingeruserinfos = fingerusermapper.getallequserinfobyname (MAINSSN);}  catch  (exception e)  {// todo: handle exceptionlogger.error (" Equipmentserviceimpl flushcache error ",  e); return ;} if  (fingeruserinfos == null | |  fingeruserinfos.size ()  == 0)  {//  If there is not a redisutil.delete ( EQUIPMENTCONSTANTS.EQUIPMENT_FINGER_USER_PRE+&NBSP;MAINSSN);}  else {redisutil.set (equipmentconstants.equipment_finger_user_pre+ mainssn,  Json.tojsonstring (Fingeruserinfos));//  new data directlySwap to cache}}/*---------------then update the device cache----------*/list<equipmentinfo> equipmentinfos = null;if   (refids!=null&&refids.length!=0)  {//with ID to refresh ID information for  (string refid :  Refids)  {try {equipmentinfos = equipmentmapper.getallequipmentinfobyid (refId);}  catch  (exception e)  {// todo: handle exceptionlogger.error (" Equipmentserviceimpl flushcache error ",  e); return ;} if  (equipmentinfos == null | |  equipmentinfos.size ()  == 0)  {//  If there is one, it fails to indicate that the query data was faulted Redisutil.delete ( EQUIPMENTCONSTANTS.EQUIPMENT_EQUINFO_PRE&NBSP;+&NBSP;REFID);}  else {redisutil.set (Equipmentconstants.equipment_equinfo_pre + refid,json.tojsonstring ( Equipmentinfos));//  new data is replaced directly in the Cache}}}}


SPRING-AMQP Integrated RABBITMQ consumer configuration and code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.