org.springframework.amqp.support.converter.MessageConverter Java Examples

The following examples show how to use org.springframework.amqp.support.converter.MessageConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MessageListenerRetryTest.java    From sinavi-jfw with Apache License 2.0 6 votes vote down vote up
@Test
public void RejectAndDontRequeue例外が発生した場合はリトライが実行されない() throws Exception {
    container.setQueues(ctx.getBean("retryTestQueue", Queue.class));
    container.setMessageListener(new MessageListenerAdapter(new AmqpRejectAndDontRequeueExceptionTestHandler(), ctx.getBean(MessageConverter.class)));
    unrecoverableContainer.setQueues(ctx.getBean("unrecoverableExceptionQueue", Queue.class));
    unrecoverableContainer.setMessageListener(new MessageListenerAdapter(new UnrecoverableTestHandler(), ctx.getBean(MessageConverter.class)));
    container.start();
    unrecoverableContainer.start();
    template.convertAndSend("retry.test.exchange", "retry.test.binding", new RetryTestBean("test"));
    assertThat(unretry.await(3, TimeUnit.SECONDS), is(true));
    assertThat(unretry.getCount(), is(0L));
    assertThat(unrecover.await(3, TimeUnit.SECONDS), is(true));
    assertThat(unrecover.getCount(), is(0L));
    container.stop();
    unrecoverableContainer.stop();
}
 
Example #2
Source File: MessageListenerRetryTest.java    From sinavi-jfw with Apache License 2.0 6 votes vote down vote up
@Test
public void システム例外の場合はリトライが実行されない() throws Exception {
    container.setQueues(ctx.getBean("retryTestQueue", Queue.class));
    container.setMessageListener(new MessageListenerAdapter(new SystemExceptionTestHandler(), ctx.getBean(MessageConverter.class)));
    unrecoverableContainer.setQueues(ctx.getBean("unrecoverableExceptionQueue", Queue.class));
    unrecoverableContainer.setMessageListener(new MessageListenerAdapter(new UnrecoverableTestHandler(), ctx.getBean(MessageConverter.class)));
    container.start();
    unrecoverableContainer.start();
    template.convertAndSend("retry.test.exchange", "retry.test.binding", new RetryTestBean("test"));
    assertThat(unretry.await(3, TimeUnit.SECONDS), is(true));
    assertThat(unretry.getCount(), is(0L));
    assertThat(unrecover.await(3, TimeUnit.SECONDS), is(true));
    assertThat(unrecover.getCount(), is(0L));
    container.stop();
    unrecoverableContainer.stop();
}
 
Example #3
Source File: MessageListenerRetryTest.java    From sinavi-jfw with Apache License 2.0 6 votes vote down vote up
@Test
public void アプリケーション回復不能例外の場合はリトライが実行されない() throws Exception {
    container.setQueues(ctx.getBean("retryTestQueue", Queue.class));
    container.setMessageListener(new MessageListenerAdapter(new ApplicationUnrecoverableExceptionTestHandler(), ctx.getBean(MessageConverter.class)));
    unrecoverableContainer.setQueues(ctx.getBean("unrecoverableExceptionQueue", Queue.class));
    unrecoverableContainer.setMessageListener(new MessageListenerAdapter(new UnrecoverableTestHandler(), ctx.getBean(MessageConverter.class)));
    container.start();
    unrecoverableContainer.start();
    template.convertAndSend("retry.test.exchange", "retry.test.binding", new RetryTestBean("test"));
    assertThat(unretry.await(3, TimeUnit.SECONDS), is(true));
    assertThat(unretry.getCount(), is(0L));
    assertThat(unrecover.await(3, TimeUnit.SECONDS), is(true));
    assertThat(unrecover.getCount(), is(0L));
    container.stop();
    unrecoverableContainer.stop();
}
 
Example #4
Source File: MessageListenerRetryTest.java    From sinavi-jfw with Apache License 2.0 6 votes vote down vote up
@Test
public void 回復可能例外の場合はデフォルトの指定回数リトライが実行されて回復可能例外キューに配信される() throws Exception {
    container.setQueues(ctx.getBean("retryTestQueue", Queue.class));
    container.setMessageListener(new MessageListenerAdapter(new ApplicationRecoverableExceptionTestHandler(), ctx.getBean(MessageConverter.class)));
    recoverableContainer.setQueues(ctx.getBean("recoverableExceptionQueue", Queue.class));
    recoverableContainer.setMessageListener(new MessageListenerAdapter(new RecoverableTestHandler(), ctx.getBean(MessageConverter.class)));
    container.start();
    recoverableContainer.start();
    template.convertAndSend("retry.test.exchange", "retry.test.binding", new RetryTestBean("test"));
    assertThat(retry.await(30, TimeUnit.SECONDS), is(true));
    assertThat(retry.getCount(), is(0L));
    assertThat(recover.await(3, TimeUnit.SECONDS), is(true));
    assertThat(recover.getCount(), is(0L));
    container.stop();
    recoverableContainer.stop();
}
 
Example #5
Source File: RabbitmqFactory.java    From shine-mq with Apache License 2.0 6 votes vote down vote up
@Override
public Factory addDLX(String queueName, String exchangeName, String routingKey, Processor processor, SendTypeEnum type,
                      MessageConverter messageConverter) {
    if (processor != null) {
        msgAdapterHandler.add(exchangeName, routingKey, processor, type, messageConverter);
        if (rabbit.isListenerEnable()) {
            declareBinding(queueName, exchangeName, routingKey, true,
                    type == null ? SendTypeEnum.DIRECT.toString() : type.toString(), true);
            if (listenerContainer == null) {
                initMsgListenerAdapter();
            } else {
                listenerContainer.addQueueNames(queueName);
            }
        } else {
            declareBinding(queueName, exchangeName, routingKey, false,
                    type == null ? SendTypeEnum.DIRECT.toString() : type.toString(), true);
        }
        return this;
    } else {
        declareBinding(queueName, exchangeName, routingKey, false,
                type == null ? SendTypeEnum.DIRECT.toString() : type.toString(), true);
        return this;
    }
}
 
Example #6
Source File: RabbitmqFactory.java    From shine-mq with Apache License 2.0 6 votes vote down vote up
@Override
public Factory add(String queueName, String exchangeName, String routingKey, Processor processor, SendTypeEnum type,
                   MessageConverter messageConverter) {
    if (processor != null) {
        msgAdapterHandler.add(exchangeName, routingKey, processor, type, messageConverter);
        if (rabbit.isListenerEnable()) {
            declareBinding(queueName, exchangeName, routingKey, true,
                    type == null ? SendTypeEnum.DIRECT.toString() : type.toString(), false);
            if (listenerContainer == null) {
                initMsgListenerAdapter();
            } else {
                listenerContainer.addQueueNames(queueName);
            }
        } else {
            declareBinding(queueName, exchangeName, routingKey, false,
                    type == null ? SendTypeEnum.DIRECT.toString() : type.toString(), false);
        }
        return this;
    } else {
        declareBinding(queueName, exchangeName, routingKey, false,
                type == null ? SendTypeEnum.DIRECT.toString() : type.toString(), false);
        return this;
    }
}
 
Example #7
Source File: RabbitAutoConfiguration.java    From easy-rabbitmq with Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleMessageListenerContainer simpleMessageListenerContainer(MessageConverter converter,
    ConnectionFactory connectionFactory, @Autowired(required = false) List<Consumer<?>> list) {
  SimpleMessageListenerContainer container =
      new SimpleMessageListenerContainer(connectionFactory);
  init(list);
  Receiver receiver = new Receiver(converter);
  container.setMessageListener(new MessageListenerAdapter(receiver, converter));
  container.setQueueNames(queueNames.toArray(new String[0]));
  container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
  container.setPrefetchCount(prefetchCount);
  container.setTxSize(txSize);
  return container;
}
 
Example #8
Source File: AmqpContextConfigTest.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
@Test
public void 値がオーバライドされて指定のインスタンスがDIコンテナに登録される() {
    assertThat(config.host, is("192.168.10.10"));
    assertThat(config.port, is("5673"));
    assertThat(config.username, is("jfw"));
    assertThat(config.password, is("jfw"));
    assertThat(config.channelCacheSize, is(100));
    assertThat(context.getBean(ConnectionFactory.class), is(notNullValue()));
    assertThat(context.getBean(RabbitTemplate.class), is(notNullValue()));
    assertThat(context.getBean(MessageConverter.class), is(notNullValue()));
}
 
Example #9
Source File: AmqpContextConfigTest.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
@Test
public void デフォルト値が設定されて指定のインスタンスがDIコンテナに登録される() {
    assertThat(config.host, is("127.0.0.1"));
    assertThat(config.port, is("5672"));
    assertThat(config.username, is("guest"));
    assertThat(config.password, is("guest"));
    assertThat(config.channelCacheSize, is(10));
    assertThat(context.getBean(ConnectionFactory.class), is(notNullValue()));
    assertThat(context.getBean(RabbitTemplate.class), is(notNullValue()));
    assertThat(context.getBean(MessageConverter.class), is(notNullValue()));
}
 
Example #10
Source File: RabbitMqConfiguration.java    From scraping-microservice-java-python-rabbitmq with Apache License 2.0 5 votes vote down vote up
@Bean
public MessageConverter jsonMessageConverter()
{
    final Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
    converter.setClassMapper(classMapper());
    return converter;
}
 
Example #11
Source File: RabbitmqTemplate.java    From shine-mq with Apache License 2.0 5 votes vote down vote up
private Object send(String exchangeName, Object msg, MessageConverter messageConverter, SendTypeEnum type,
                    String routingKey, int expiration, int priority) throws Exception {
    check(exchangeName, routingKey);

    Object obj = null;
    String msgId = UUID.randomUUID().toString();
    EventMessage eventMessage = new EventMessage(exchangeName, routingKey, type.toString(), msg, null, msgId);
    MessageProperties messageProperties = new MessageProperties();
    //过期时间
    if (expiration > 0) {
        messageProperties.setExpiration(String.valueOf(expiration));
    }
    //消息优先级
    if (priority > 0) {
        messageProperties.setPriority(priority);
    }
    messageProperties.setMessageId(msgId);
    // 设置消息持久化
    messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
    Message message = messageConverter.toMessage(eventMessage, messageProperties);
    rabbitmqFactory.setCorrelationData(msgId, null, eventMessage, null);
    try {
        if (SendTypeEnum.RPC.equals(type)) {
            obj = eventAmqpTemplate.convertSendAndReceive(routingKey, message);
        } else {
            eventAmqpTemplate.send(exchangeName, routingKey, message);
        }
    } catch (AmqpException e) {
        logger.error("send event fail. Event Message : [{}]", eventMessage, e);
        throw new Exception("send event fail", e);
    }
    return obj;
}
 
Example #12
Source File: MessageAdapterHandler.java    From shine-mq with Apache License 2.0 5 votes vote down vote up
protected void add(String exchangeName, String routingKey, Processor processor, SendTypeEnum type,
                   MessageConverter messageConverter) {

    Objects.requireNonNull(exchangeName, "The exchangeName is empty.");
    Objects.requireNonNull(messageConverter, "The messageConverter is empty.");
    Objects.requireNonNull(routingKey, "The routingKey is empty.");

    ProcessorWrap pw = new ProcessorWrap(messageConverter, processor);
    ProcessorWrap oldProcessorWrap = map.putIfAbsent(exchangeName + "_" + routingKey + "_" +
            (type == null ? SendTypeEnum.DIRECT.toString() : type.toString()), pw);
    if (oldProcessorWrap != null) {
        logger.warn("The processor of this queue and exchange exists");
    }
}
 
Example #13
Source File: AmqpMessagingApplication.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
		ConnectionFactory connectionFactory, MessageConverter messageConverter) {
	SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
	factory.setConnectionFactory(connectionFactory);
	factory.setConcurrentConsumers(3);
	factory.setMaxConcurrentConsumers(10);
	factory.setMessageConverter(messageConverter);
	return factory;
}
 
Example #14
Source File: BusConfig.java    From SpringCloud with Apache License 2.0 4 votes vote down vote up
@Bean
MessageListenerAdapter messageListenerAdapter(BusReceiver busReceiver, MessageConverter messageConverter) {
    log.info("new listener");
    return new MessageListenerAdapter(busReceiver, messageConverter);
}
 
Example #15
Source File: AmqpMessageBrokerConfiguration.java    From piper with Apache License 2.0 4 votes vote down vote up
@Bean 
MessageConverter jacksonAmqpMessageConverter(ObjectMapper aObjectMapper) {
  return new Jackson2JsonMessageConverter(aObjectMapper);
}
 
Example #16
Source File: RabbitMqConfiguration.java    From shipping with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter jsonMessageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example #17
Source File: RabbitConfiguration.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example #18
Source File: BusConfig.java    From SpringCloud with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    return new ContentTypeDelegatingMessageConverter(new Jackson2JsonMessageConverter(objectMapper));
}
 
Example #19
Source File: BusConfig.java    From SpringCloud with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    return new ContentTypeDelegatingMessageConverter(new Jackson2JsonMessageConverter(objectMapper));
}
 
Example #20
Source File: BusConfig.java    From SpringCloud with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    return new ContentTypeDelegatingMessageConverter(new Jackson2JsonMessageConverter(objectMapper));
}
 
Example #21
Source File: BusConfig.java    From SpringCloud with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    return new ContentTypeDelegatingMessageConverter(new Jackson2JsonMessageConverter(objectMapper));
}
 
Example #22
Source File: MQConsumerConfig.java    From lemon-rabbitmq with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example #23
Source File: RabbitTemplateConfig.java    From spring-boot-tutorials with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter jackson2JsonMessageConverter() {
    Jackson2JsonMessageConverter bean = new Jackson2JsonMessageConverter();
    return bean;
}
 
Example #24
Source File: RabbitTemplateConfig.java    From spring-boot-tutorials with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter jackson2JsonMessageConverter() {
    Jackson2JsonMessageConverter bean = new Jackson2JsonMessageConverter();
    return bean;
}
 
Example #25
Source File: AmqpMessagingApplication.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageListenerAdapter messageListenerAdapter(
		MessageSubscriber messageSubscriber, MessageConverter messageConverter) {
	return new MessageListenerAdapter(messageSubscriber, messageConverter);
}
 
Example #26
Source File: ContractVerifierAmqpAutoConfiguration.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
ContractVerifierHelper(MessageVerifier<Message> exchange,
		MessageConverter messageConverter) {
	super(exchange);
	this.messageConverter = messageConverter;
}
 
Example #27
Source File: AmqpConfig.java    From demo_springboot_rabbitmq with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter jsonMessageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example #28
Source File: BaseAmqpService.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
protected MessageConverter getMessageConverter() {
    return rabbitTemplate.getMessageConverter();
}
 
Example #29
Source File: SpringMessageConverterAdapter.java    From zxl with Apache License 2.0 4 votes vote down vote up
public void setMessageConverter(MessageConverter messageConverter) {
	this.messageConverter = messageConverter;
}
 
Example #30
Source File: RabbitMQConfig.java    From jakduk-api with MIT License 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter(objectMapper);
}