The queue configuration, producer configuration, consumer configuration, and general MQ configuration are the same.
Reply-timeout is an expiration date and can be written to a configuration file.
<rabbit:template id= "template" message-converter= messageconverter "connection-factory=" ConnectionFactory
"Reply-timeout=" retry-template= "Retrytemplate" exchange= "
${rabbitmq.direct.exchange}" />
1. Sample producer Code
RPC calls are made using the Template.convertsendandreceive method, in this case direct mode is used, or fanout and topic are used.
public class Mqblocking extends Basetask {
@Override
protected void execute (Final schedulejob schedulejob, final String ID) {
rabbittemplate template = (rabbittemplate) springcontextholder.getbean ("template");
Baseresponse obj = (baseresponse) template.convertsendandreceive (Template.getexchange (), "ccs.binding." + Schedulejob.getmqname (), ID, new Messagepostprocessor () {
@Override public message
postprocessmessage ( Message message) throws Amqpexception {
message.getmessageproperties (). SetHeader ("JobName", Schedulejob.getname ());
return message;}}
);
}
2. Consumer code example
@RabbitListener (queues = "ccs.queue.blocking") public
baseresponse helloblocking (String ID, @Header ("JobName") String jobName) {
System.out.println ("Received Request for ID" + ID);
SYSTEM.OUT.PRINTLN ("Received Request for Job name" + jobName);
Returns the execution result (success, failure) and ID
baseresponse response = new Baseresponse (true, id);
return response;
}
3. How the code is implemented
Because the low version of MQ does not support annotations, you can configure the listener using code only
Consumer Configuration
<!--message Converter-->
<bean id= "Messageconverter" class= " Org.springframework.amqp.support.converter.JsonMessageConverter "/>
<!--queue configuration-->
<rabbit: Queue name= "ccs.queue.blocking"/>
<rabbit:direct-exchange name= "Ccs.direct.exchange" >
< rabbit:bindings>
<rabbit:binding queue= "ccs.queue.blocking" key= ccs.binding.blocking "/>"
</ rabbit:bindings>
</rabbit:direct-exchange>
<!--listener configuration-->
<rabbit: Listener-container connection-factory= "ConnectionFactory" message-converter= "Jsonmessageconverter" >
< Rabbit:listener ref= "Queuehandler" method= "Handle" queues= "ccs.queue.blocking"/> </rabbit
: Listener-container>
Consumer Code
Package Com.whty.bwjf.tsp.cm.controller;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.amqp.core.Message;
Import org.springframework.amqp.core.MessageProperties;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;
Import Com.whty.bwjf.framework.core.util.JsonMessageConverter;
Import Com.whty.framework.base.web.mvc.view.BaseResponse;
@Component public class Queuehandler {@Autowired private jsonmessageconverter messageconverter;
Logger Logger = Loggerfactory.getlogger (Queuehandler.class); Public message handle {System.out.println ("Received Request for JobName" + Message.getmessa
Geproperties () getheaders (). Get ("JobName"));
String id = (string) messageconverter.frommessage (message);
SYSTEM.OUT.PRINTLN ("Received Request for ID" + ID);
Returns the execution result (success, failure) and ID baseresponse response = new Baseresponse (true, id); Return Messageconverter.tomessage (response, New messageproperties ()); }
}
PS: It is possible that the first time you add these configurations, the message passes through the converter, but converter is converted to an entity type and cannot be entered into the handle.
You can configure an empty converter to resolve this problem
Package com.whty.bwjf.framework.core.util;
Import java.io.UnsupportedEncodingException;
Import Org.springframework.amqp.core.Message;
Import org.springframework.amqp.core.MessageProperties;
Import Org.springframework.amqp.support.converter.AbstractMessageConverter;
Import org.springframework.amqp.support.converter.MessageConversionException;
Import Com.alibaba.fastjson.JSON;
@Component public class Originmessageconverter extends Abstractmessageconverter {public originmessageconverter () {
Super (); } public void Setdefaultcharset (String defaultcharset) {This.defaultcharset = (Defaultcharset!= null)? def
Aultcharset:default_charset;
The public Object frommessage (message) throws the messageconversionexception {return message;
} protected Message CreateMessage (Object objecttoconvert, messageproperties messageproperties) Throws Messageconversionexception {return (message) Objecttoconvert; }
}
<rabbit:listener-container connection-factory= "ConnectionFactory" message-converter=
"Originmessageconverter" > <rabbit:listener ref= "queuehandler" method= "Handle" queues= "Crm.hb.queue"/> </rabbit:listener-container>