在Spring Boot中使用rabitMQ接收消息时将一个字段设为null

时间:2019-01-08 12:38:21

标签: spring-boot rabbitmq

我有两个正在运行的应用程序,使用RabbitMQ实现实时通知,我能够接收消息,但只有一个字段为空。我要说的是我到目前为止所写的代码,这只是为此而开始。

这是我的域类:

public class Notification implements Serializable{

    private String msg;
    private String productId;
    private double amount;

    public Notification() {
    }

    public Notification(String msg, String productId, double amount) {
        this.msg = msg;
        this.productId = productId;
        this.amount = amount;
    }

    /**
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     * @param msg the msg to set
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * @return the productId
     */
    public String getProductId() {
        return productId;
    }

    /**
     * @param productId the productId to set
     */
    public void setProductId(String productId) {
        this.productId = productId;
    }

    /**
     * @return the amount
     */
    public double getAmount() {
        return amount;
    }

    /**
     * @param amount the amount to set
     */
    public void setAmount(double amount) {
        this.amount = amount;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Notification{" +
                "Message='" + msg + '\'' +
                ", productId='" + productId + '\'' +
                ", amount=" + amount +
                '}';
    }

}

My Controller Class : 



 @Controller
    @RequestMapping("msg")
    public class MessageController {

        static final Logger logger = LoggerFactory.getLogger(MessageController.class);

        private final OrderMessageSender orderMessageSender;

        @Autowired
        public MessageController(OrderMessageSender orderMessageSender) {
            this.orderMessageSender = orderMessageSender;
        }

        @GetMapping("/amqp")
        public String getMessagePage() {
            return "message";
        }

        @PostMapping("/sendMsg")
        public String handleMessage(Notification notifyMsg, RedirectAttributes redirectAttributes) {
            orderMessageSender.sendNotificationToClient(notifyMsg);
            logger.info("Notification recieved : "+notifyMsg.getAmount());
            redirectAttributes.addFlashAttribute("message", "Order message sent successfully");
            return "redirect:/";
        }
    }

Following is RabbitMQ Config Class : 

@配置 公共类RabbitConfig实现了RabbitListenerConfigurer {

public static final String QUEUE = "aem-queue";
public static final String QUEUE_DEAD_ORDERS = "dead-aem-queue";
public static final String EXCHANGE_ORDERS = "aem-exchange";

@Bean
Queue ordersQueue() {

    return QueueBuilder.durable(QUEUE)
            .withArgument("x-dead-letter-exchange", "")
            .withArgument("x-dead-letter-routing-key", QUEUE_DEAD_ORDERS)
            .withArgument("x-message-ttl", 5000)
            .build();
}

@Bean
Queue deadLetterQueue() {
    return QueueBuilder.durable(QUEUE_DEAD_ORDERS).build();
}

@Bean
Exchange ordersExchange() {
    return ExchangeBuilder.topicExchange(EXCHANGE_ORDERS).build();
}

@Bean
Binding binding(Queue ordersQueue, TopicExchange ordersExchange) {
    return BindingBuilder.bind(ordersQueue).to(ordersExchange).with(QUEUE);
}

@Bean
public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
    final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
    rabbitTemplate.setMessageConverter(producerJackson2MessageConverter());
    return rabbitTemplate;
}

@Bean
public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
    return new Jackson2JsonMessageConverter();
}

@Bean
MessageHandlerMethodFactory messageHandlerMethodFactory() {
    DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
    messageHandlerMethodFactory.setMessageConverter(consumerJackson2MessageConverter());
    return messageHandlerMethodFactory;
}

@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
    registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
}

@Bean
public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
    return new MappingJackson2MessageConverter();
}

}

以下是侦听器: @零件 公共类OrderMessageListener {

static final Logger logger = LoggerFactory.getLogger(OrderMessageListener.class);

@RabbitListener(queues = RabbitConfig.QUEUE)    
public void processNotification(Notification msg) {
    if (msg.getMsg() != null) {
        logger.info("Order Received: "+msg.getMsg());
    }else {
        logger.info("Please refer the sent msg, it migth be null or not convertible...");
        logger.info("Order Received: "+msg);
    }

}

}

以下是发件人: @服务 公共类OrderMessageSender {

static final Logger logger = LoggerFactory.getLogger(OrderMessageSender.class);

private final RabbitTemplate rabbitTemplate;
private final ObjectMapper objectMapper;

@Autowired
public OrderMessageSender(RabbitTemplate rabbitTemplate, ObjectMapper objectMapper) {
    this.rabbitTemplate = rabbitTemplate;
    this.objectMapper = objectMapper;
}

public void sendNotificationToClient(Notification notifyMsg) {
    this.rabbitTemplate.convertAndSend(RabbitConfig.QUEUE, notifyMsg);
    logger.info("Message send successfully ..... to client");
    /*
    try {
        String orderJson = objectMapper.writeValueAsString(order);
        Message message = MessageBuilder
                            .withBody(orderJson.getBytes())
                            .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                            .build();
        this.rabbitTemplate.convertAndSend(RabbitConfig.QUEUE_ORDERS, message);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    */
}

}

0 个答案:

没有答案