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

The following examples show how to use org.springframework.amqp.support.converter.MessageConverter. These examples are extracted from open source projects. 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 Project: shine-mq   Source File: RabbitmqFactory.java    License: 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 2
Source Project: shine-mq   Source File: RabbitmqFactory.java    License: 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 3
Source Project: sinavi-jfw   Source File: MessageListenerRetryTest.java    License: 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 4
Source Project: sinavi-jfw   Source File: MessageListenerRetryTest.java    License: 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 5
Source Project: sinavi-jfw   Source File: MessageListenerRetryTest.java    License: 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 6
Source Project: sinavi-jfw   Source File: MessageListenerRetryTest.java    License: 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 7
Source Project: easy-rabbitmq   Source File: RabbitAutoConfiguration.java    License: 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 Project: shine-mq   Source File: MessageAdapterHandler.java    License: 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 9
Source Project: shine-mq   Source File: RabbitmqTemplate.java    License: 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 10
@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 11
@Bean
public MessageConverter jsonMessageConverter()
{
    final Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
    converter.setClassMapper(classMapper());
    return converter;
}
 
Example 12
Source Project: sinavi-jfw   Source File: AmqpContextConfigTest.java    License: 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 13
Source Project: sinavi-jfw   Source File: AmqpContextConfigTest.java    License: 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 14
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example 15
Source Project: mogu_blog_v2   Source File: RabbitMqConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example 16
Source Project: mogu_blog_v2   Source File: RabbitMqConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example 17
Source Project: mogu_blog_v2   Source File: RabbitMqConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example 18
Source Project: easy-rabbitmq   Source File: Receiver.java    License: Apache License 2.0 4 votes vote down vote up
public Receiver(MessageConverter converter) {
  this.converter = converter;
}
 
Example 19
Source Project: easy-rabbitmq   Source File: RabbitAutoConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(MessageConverter.class)
public MessageConverter messageConverter() {
  return new CustomMessageConverter();
}
 
Example 20
Source Project: summerframework   Source File: RabbitAutoConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
public RabbitTemplateConfiguration(ObjectProvider<MessageConverter> messageConverter,
    RabbitProperties properties) {
    this.messageConverter = messageConverter;
    this.properties = properties;
}
 
Example 21
RabbitAnnotationDrivenConfiguration(ObjectProvider<MessageConverter> messageConverter,
    ObjectProvider<MessageRecoverer> messageRecoverer, RabbitProperties properties) {
    this.messageConverter = messageConverter;
    this.messageRecoverer = messageRecoverer;
    this.properties = properties;
}
 
Example 22
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example 23
@Bean
public MessageConverter messageConverter() {
	return new Jackson2JsonMessageConverter();
}
 
Example 24
Source Project: shine-mq   Source File: MessageAdapterHandler.java    License: Apache License 2.0 4 votes vote down vote up
protected ProcessorWrap(MessageConverter messageConverter, Processor processor) {
    this.messageConverter = messageConverter;
    this.processor = processor;
}
 
Example 25
Source Project: shine-mq   Source File: RabbitmqTemplate.java    License: Apache License 2.0 4 votes vote down vote up
public RabbitmqTemplate(RabbitmqFactory rabbitmqFactory, AmqpTemplate amqpTemplate,
                        MessageConverter messageConverter) {
    this.rabbitmqFactory = rabbitmqFactory;
    this.eventAmqpTemplate = amqpTemplate;
    this.messageConverter = messageConverter;
}
 
Example 26
@Bean
public MessageConverter jsonMessageConverter() {
	return new Jackson2JsonMessageConverter();
}
 
Example 27
@Bean
public MessageConverter jsonMessageConverter() {
	return new Jackson2JsonMessageConverter();
}
 
Example 28
Source Project: SpringBootBucket   Source File: RabbitConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public MessageConverter jsonMessageConverter(){
    return new Jackson2JsonMessageConverter();
}
 
Example 29
Source Project: SpringBootBucket   Source File: RabbitConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public MessageConverter jsonMessageConverter(){
    return new Jackson2JsonMessageConverter();
}
 
Example 30
Source Project: articles   Source File: JsonConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter jsonMessageConverter() {
    return new Jackson2JsonMessageConverter();
}