-
Notifications
You must be signed in to change notification settings - Fork 626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi-consumers binding different parameters #879
Comments
Your question is not clear. What is You need to show your configuration. |
message is the object of MessageBody,MessageBody is class,which contain timestamp, query and other fields; My configuration is as follows: package com.lzz.config;
import com.lzz.config.messageconverter.CustomJsonMessageConverter;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableRabbit
public class RabbitConfig {
@Bean
public Queue time(){
return new Queue("time");
}
@Bean
public Queue date(){
return new Queue("date");
}
@Bean
public Queue chars(){
return new Queue("chars");
}
@Bean
public TopicExchange exchange(){
return new TopicExchange("exchange");
}
@Bean
public Binding bindingTime(Queue time,TopicExchange exchange){
return BindingBuilder.bind(time).to(exchange).with("#.T.#");
}
@Bean
public Binding bindingDate(Queue date, TopicExchange exchange){
return BindingBuilder.bind(date).to(exchange).with("#.D.#");
}
@Bean
public Binding bindingChar(Queue chars, TopicExchange exchange){
return BindingBuilder.bind(chars).to(exchange).with("#.C.#");
}
@Bean
public AmqpTemplate amqpTemplate(final ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
return rabbitTemplate;
}
} ReceveMessage: @Service
@Slf4j
@RabbitListener(queues = "time")
public class TimeStatisticService {
/**
* @param message
*/
@RabbitHandler
public void messageReceiver(MessageBody message){
log.info("time is {}",message.getMessage());
}
} messagebody: @Data
@AllArgsConstructor
public class MessageBody{
private String timestamp;
private String query;
} |
You also need to show the Also please read about GitHub markup. |
so sorry,the listeneris as follows: package com.lzz.service.statistic.listener;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import java.io.IOException;
@Service
@Slf4j
@RabbitListener(queues = "date")
public class DateStatisticService {
@RabbitHandler
public void messageReceiver(Message message, Channel channel) throws IOException {
try{
log.info("date is {}",message);
log.info("tag is {}",message.getMessageProperties().getDeliveryTag());
}catch (Exception e){
log.error("error is {}",e);
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
}
}
} In addition to, messageBody should be: @Data
@AllArgsConstructor
public class MessageBody{
private String timestamp;
private String query;
private String message;
} |
Again; please use proper markup - see my edits.
@RabbitListener(...)
public class Foo {
@RabbitHandler
public void handleFoo(Foo foo) {...}
@RabbitHandler
public void handleBar(Bar bar) {...}
} So we can determine which method to call after the payload has been converted. If you want the raw message, put @Component
class Foo {
@RabbitListener(queues = "foo")
public void handle(Message message) {
System.out.println(message);
}
} We will change the documentation to make that clearer. |
Thank you for your help |
version:
spring-boot 2.0.4.RELEASE
when a message is routed to Multi-consumers, a consumer needs to deserialize message, but another consumer receives it directly,which causes an exception,such as
SendMessage:
amqpTemplate.convertAndSend("exchange","T.D",message);
ReceveMessage1:
@RabbitHandler public void messageReceiver(MessageBody message){ log.info("time is {}",message.getMessage()); }
ReceveMessage2:
@RabbitHandler public void messageReceiver(Message message){ log.info("date is {}",message); }
Expected two ways can be used simultaneously
The text was updated successfully, but these errors were encountered: