org.springframework.amqp.core.AcknowledgeMode Java Examples

The following examples show how to use org.springframework.amqp.core.AcknowledgeMode. 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: AmqpConfig.java    From myth with Apache License 2.0 6 votes vote down vote up
/**
 * Message container simple message listener container.
 *
 * @return the simple message listener container
 */
@Bean
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "host")
public SimpleMessageListenerContainer messageContainer() {
    SimpleMessageListenerContainer container =
            new SimpleMessageListenerContainer(connectionFactory);
    container.setQueues(queue());
    container.setExposeListenerChannel(true);
    container.setMaxConcurrentConsumers(2);
    container.setConcurrentConsumers(1);
    //设置确认模式手工确认
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    container.setMessageListener((ChannelAwareMessageListener) (message, channel) -> {
        byte[] messageBody = message.getBody();
        LOGGER.debug("motan 框架接收到的消息");
        //确认消息成功消费
        final Boolean success = mythMqReceiveService.processMessage(messageBody);
        if (success) {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    });
    return container;
}
 
Example #2
Source File: AmqpConfig.java    From myth with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "host")
public SimpleMessageListenerContainer messageContainer() {
    SimpleMessageListenerContainer container =
            new SimpleMessageListenerContainer(connectionFactory);
    container.setQueues(queue());
    container.setExposeListenerChannel(true);
    container.setMaxConcurrentConsumers(1);
    container.setConcurrentConsumers(1);
    //设置确认模式手工确认
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    container.setMessageListener((ChannelAwareMessageListener) (message, channel) -> {
        byte[] messageBody = message.getBody();
        //确认消息成功消费
        final Boolean success = mythMqReceiveService.processMessage(messageBody);
        if (success) {
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    return container;
}
 
Example #3
Source File: AmqpConfig.java    From myth with Apache License 2.0 6 votes vote down vote up
/**
 * Message container simple message listener container.
 *
 * @return the simple message listener container
 */
@Bean
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "host")
public SimpleMessageListenerContainer messageContainer() {
    SimpleMessageListenerContainer container =
            new SimpleMessageListenerContainer(connectionFactory);
    container.setQueues(queue());
    container.setExposeListenerChannel(true);
    container.setMaxConcurrentConsumers(3);
    container.setConcurrentConsumers(1);
    //设置确认模式手工确认
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    container.setMessageListener((ChannelAwareMessageListener) (message, channel) -> {
        byte[] messageBody = message.getBody();
        LogUtil.debug(LOGGER, () -> "springcloud  account服务  amqp接收消息");
        //确认消息成功消费
        final Boolean success = mythMqReceiveService.processMessage(messageBody);
        if (success) {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    });
    return container;
}
 
Example #4
Source File: BaseConfiguration.java    From common-project with Apache License 2.0 5 votes vote down vote up
/**
 * 配置接收消息的MessageConverter
 *
 * @param connectionFactory 连接工厂
 * @return
 */
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setPrefetchCount(50);
    factory.setConcurrentConsumers(5);
    factory.setMessageConverter(new ProtobufMessageConverter());
    factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
    return factory;
}
 
Example #5
Source File: SpringIntegrationConfig.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "destroy")
public SimpleMessageListenerContainer simpleMessageListenerContainer() {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueues(queue());
    container.setMessageListener(loggingMessageListenerAdapter());
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);

    return container;
}
 
Example #6
Source File: RabbitSourceTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
	Advice[] adviceChain = TestUtils.getPropertyValue(this.container, "adviceChain", Advice[].class);
	assertEquals(0, adviceChain.length);
	assertTrue(TestUtils.getPropertyValue(this.container, "transactional", Boolean.class));
	assertEquals(AcknowledgeMode.AUTO, TestUtils.getPropertyValue(this.container, "acknowledgeMode"));
	assertEquals("scsapp-testq", this.container.getQueueNames()[0]);
	assertEquals("scsapp-testq2", this.container.getQueueNames()[1]);
}
 
Example #7
Source File: RabbitSourceTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
	Advice[] adviceChain = TestUtils.getPropertyValue(this.container, "adviceChain", Advice[].class);
	assertEquals(1, adviceChain.length);
	RetryTemplate retryTemplate = TestUtils.getPropertyValue(adviceChain[0], "retryOperations",
			RetryTemplate.class);
	assertEquals(5, TestUtils.getPropertyValue(retryTemplate, "retryPolicy.maxAttempts"));
	assertEquals(123L, TestUtils.getPropertyValue(retryTemplate, "backOffPolicy.initialInterval"));
	assertEquals(345L, TestUtils.getPropertyValue(retryTemplate, "backOffPolicy.maxInterval"));
	assertEquals(1.5, TestUtils.getPropertyValue(retryTemplate, "backOffPolicy.multiplier"));
	assertEquals("scsapp-testq", this.container.getQueueNames()[0]);
	assertFalse(TestUtils.getPropertyValue(this.container, "defaultRequeueRejected", Boolean.class));
	assertEquals(2, TestUtils.getPropertyValue(this.container, "concurrentConsumers"));
	assertEquals(3, TestUtils.getPropertyValue(this.container, "maxConcurrentConsumers"));
	assertEquals(AcknowledgeMode.NONE, TestUtils.getPropertyValue(this.container, "acknowledgeMode"));
	assertEquals(10, TestUtils.getPropertyValue(this.container, "prefetchCount"));
	assertEquals(5, TestUtils.getPropertyValue(this.container, "txSize"));

	this.rabbitTemplate.convertAndSend("", "scsapp-testq", "foo", new MessagePostProcessor() {

		@Override
		public org.springframework.amqp.core.Message postProcessMessage(
				org.springframework.amqp.core.Message message) throws AmqpException {
			message.getMessageProperties().getHeaders().put("bar", "baz");
			return message;
		}

	});
	Message<?> out = this.messageCollector.forChannel(this.channels.output()).poll(10,  TimeUnit.SECONDS);
	assertNotNull(out);
	assertEquals("foo", out.getPayload());
	assertEquals("baz", out.getHeaders().get("bar"));
	assertNull(out.getHeaders().get(AmqpHeaders.DELIVERY_MODE));
}
 
Example #8
Source File: RabbitChannelDefinitionProcessor.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected AcknowledgeMode resolveAckMode(RabbitInboundChannelModel channelDefinition) {
    String ackModeAttr = channelDefinition.getAckMode();
    if (StringUtils.hasText(ackModeAttr)) {
        Object ackMode = resolveExpression(ackModeAttr);
        if (ackMode instanceof String) {
            return AcknowledgeMode.valueOf((String) ackMode);
        } else if (ackMode instanceof AcknowledgeMode) {
            return (AcknowledgeMode) ackMode;
        } else {
            throw new IllegalArgumentException("ackMode in definition [ " + channelDefinition + " ] must resolve to a String or AcknowledgeMode");
        }
    } else {
        return null;
    }
}
 
Example #9
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
private SimpleMessageListenerContainer verifyContainer(Lifecycle endpoint) {
	SimpleMessageListenerContainer container;
	RetryTemplate retry;
	container = TestUtils.getPropertyValue(endpoint, "messageListenerContainer",
			SimpleMessageListenerContainer.class);
	assertThat(container.getAcknowledgeMode()).isEqualTo(AcknowledgeMode.NONE);
	assertThat(container.getQueueNames()[0]).startsWith("foo.props.0");
	assertThat(TestUtils.getPropertyValue(container, "transactional", Boolean.class))
			.isFalse();
	assertThat(TestUtils.getPropertyValue(container, "concurrentConsumers"))
			.isEqualTo(2);
	assertThat(TestUtils.getPropertyValue(container, "maxConcurrentConsumers"))
			.isEqualTo(3);
	assertThat(TestUtils.getPropertyValue(container, "defaultRequeueRejected",
			Boolean.class)).isFalse();
	assertThat(TestUtils.getPropertyValue(container, "prefetchCount")).isEqualTo(20);
	assertThat(TestUtils.getPropertyValue(container, "batchSize")).isEqualTo(10);
	retry = TestUtils.getPropertyValue(endpoint, "retryTemplate",
			RetryTemplate.class);
	assertThat(TestUtils.getPropertyValue(retry, "retryPolicy.maxAttempts"))
			.isEqualTo(23);
	assertThat(TestUtils.getPropertyValue(retry, "backOffPolicy.initialInterval"))
			.isEqualTo(2000L);
	assertThat(TestUtils.getPropertyValue(retry, "backOffPolicy.maxInterval"))
			.isEqualTo(20000L);
	assertThat(TestUtils.getPropertyValue(retry, "backOffPolicy.multiplier"))
			.isEqualTo(5.0);

	List<?> requestMatchers = TestUtils.getPropertyValue(endpoint,
			"headerMapper.requestHeaderMatcher.matchers", List.class);
	assertThat(requestMatchers).hasSize(1);
	assertThat(TestUtils.getPropertyValue(requestMatchers.get(0), "pattern"))
			.isEqualTo("foo");

	return container;
}
 
Example #10
Source File: RabbitMQRouterConfig.java    From konker-platform with Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
    factory.setConcurrentConsumers(1);
    factory.setMaxConcurrentConsumers(20);

    Advice[] adviceChain = new Advice[] { interceptor() };
    factory.setAdviceChain(adviceChain);
    return factory;
}
 
Example #11
Source File: AmqpConfiguration.java    From SkyEye with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public SimpleMessageListenerContainer messageContainer(ConnectionFactory connectionFactory, MessageListenerAdapter messageListenerAdapter) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueues(this.queue);
    container.setExposeListenerChannel(true);
    container.setMaxConcurrentConsumers(this.maxConcurrentConsumers);
    container.setConcurrentConsumers(this.concurrentConsumers);
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);
    container.setMessageListener(messageListenerAdapter);
    return container;
}
 
Example #12
Source File: RabbitConfig.java    From notes with Apache License 2.0 5 votes vote down vote up
/**
     * 创建制定的 监听容器
     *
     * @param queueName  监听的队列名字
     * @param listenerChannel 设置是否将监听的频道 公开给已注册的
     * @param PrefetchCount  告诉代理一次请求多少条消息过来
     * @param ConcurrentConsumers  制定创建多少个并发的消费者数量
     * @param acknowledgeMode  消息确认模式
     * @param listener 监听器
     * @return org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer
     * @author fruiqi
     * @date 19-2-11 下午4:13
     **/
    public SimpleMessageListenerContainer setSimpleMessageListenerContainer(String queueName, boolean listenerChannel,
                                                                            int PrefetchCount, int ConcurrentConsumers,
                                                                            AcknowledgeMode acknowledgeMode,
                                                                            ChannelAwareMessageListener listener) throws Exception {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(getConnection());
//        container.setQueueNames(getMACAddressRrefix(queueName));
        container.setQueueNames(queueName);
        container.setExposeListenerChannel(listenerChannel);
        container.setPrefetchCount(PrefetchCount);
        container.setConcurrentConsumers(ConcurrentConsumers);
        container.setAcknowledgeMode(acknowledgeMode);
        container.setMessageListener(listener);
        return container;
    }
 
Example #13
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 #14
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Test
public void testConsumerProperties() throws Exception {
	RabbitTestBinder binder = getBinder();
	ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
	properties.getExtension().setRequeueRejected(true);
	properties.getExtension().setTransacted(true);
	properties.getExtension().setExclusive(true);
	properties.getExtension().setMissingQueuesFatal(true);
	properties.getExtension().setFailedDeclarationRetryInterval(1500L);
	properties.getExtension().setQueueDeclarationRetries(23);
	Binding<MessageChannel> consumerBinding = binder.bindConsumer("props.0", null,
			createBindableChannel("input", new BindingProperties()), properties);
	Lifecycle endpoint = extractEndpoint(consumerBinding);
	SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint,
			"messageListenerContainer", SimpleMessageListenerContainer.class);
	assertThat(container.getAcknowledgeMode()).isEqualTo(AcknowledgeMode.AUTO);
	assertThat(container.getQueueNames()[0])
			.startsWith(properties.getExtension().getPrefix());
	assertThat(TestUtils.getPropertyValue(container, "transactional", Boolean.class))
			.isTrue();
	assertThat(TestUtils.getPropertyValue(container, "exclusive", Boolean.class))
			.isTrue();
	assertThat(TestUtils.getPropertyValue(container, "concurrentConsumers"))
			.isEqualTo(1);
	assertThat(TestUtils.getPropertyValue(container, "maxConcurrentConsumers"))
			.isNull();
	assertThat(TestUtils.getPropertyValue(container, "defaultRequeueRejected",
			Boolean.class)).isTrue();
	assertThat(TestUtils.getPropertyValue(container, "prefetchCount")).isEqualTo(1);
	assertThat(TestUtils.getPropertyValue(container, "batchSize")).isEqualTo(1);
	assertThat(TestUtils.getPropertyValue(container, "missingQueuesFatal",
			Boolean.class)).isTrue();
	assertThat(
			TestUtils.getPropertyValue(container, "failedDeclarationRetryInterval"))
					.isEqualTo(1500L);
	assertThat(TestUtils.getPropertyValue(container, "declarationRetries"))
			.isEqualTo(23);
	RetryTemplate retry = TestUtils.getPropertyValue(endpoint, "retryTemplate",
			RetryTemplate.class);
	assertThat(TestUtils.getPropertyValue(retry, "retryPolicy.maxAttempts"))
			.isEqualTo(3);
	assertThat(TestUtils.getPropertyValue(retry, "backOffPolicy.initialInterval"))
			.isEqualTo(1000L);
	assertThat(TestUtils.getPropertyValue(retry, "backOffPolicy.maxInterval"))
			.isEqualTo(10000L);
	assertThat(TestUtils.getPropertyValue(retry, "backOffPolicy.multiplier"))
			.isEqualTo(2.0);
	consumerBinding.unbind();
	assertThat(endpoint.isRunning()).isFalse();

	properties = createConsumerProperties();
	properties.getExtension().setAcknowledgeMode(AcknowledgeMode.NONE);
	properties.setBackOffInitialInterval(2000);
	properties.setBackOffMaxInterval(20000);
	properties.setBackOffMultiplier(5.0);
	properties.setConcurrency(2);
	properties.setMaxAttempts(23);
	properties.getExtension().setMaxConcurrency(3);
	properties.getExtension().setPrefix("foo.");
	properties.getExtension().setPrefetch(20);
	properties.getExtension().setHeaderPatterns(new String[] { "foo" });
	properties.getExtension().setTxSize(10);
	QuorumConfig quorum = properties.getExtension().getQuorum();
	quorum.setEnabled(true);
	quorum.setDeliveryLimit(10);
	quorum.setInitialGroupSize(1);
	properties.setInstanceIndex(0);
	consumerBinding = binder.bindConsumer("props.0", "test",
			createBindableChannel("input", new BindingProperties()), properties);

	endpoint = extractEndpoint(consumerBinding);
	container = verifyContainer(endpoint);

	assertThat(container.getQueueNames()[0]).isEqualTo("foo.props.0.test");

	consumerBinding.unbind();
	assertThat(endpoint.isRunning()).isFalse();
}
 
Example #15
Source File: RabbitProperties.java    From summerframework with Apache License 2.0 4 votes vote down vote up
@Deprecated
public void setAcknowledgeMode(AcknowledgeMode acknowledgeMode) {
    getSimple().setAcknowledgeMode(acknowledgeMode);
}
 
Example #16
Source File: RabbitConsumerProperties.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
public AcknowledgeMode getAcknowledgeMode() {
	return acknowledgeMode;
}
 
Example #17
Source File: RabbitConsumerProperties.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
public void setAcknowledgeMode(AcknowledgeMode acknowledgeMode) {
	Assert.notNull(acknowledgeMode, "Acknowledge mode cannot be null");
	this.acknowledgeMode = acknowledgeMode;
}
 
Example #18
Source File: RabbitProperties.java    From summerframework with Apache License 2.0 4 votes vote down vote up
@DeprecatedConfigurationProperty(replacement = "spring.rabbitmq.listener.simple.acknowledge-mode")
@Deprecated
public AcknowledgeMode getAcknowledgeMode() {
    return getSimple().getAcknowledgeMode();
}
 
Example #19
Source File: RabbitProperties.java    From summerframework with Apache License 2.0 4 votes vote down vote up
public void setAcknowledgeMode(AcknowledgeMode acknowledgeMode) {
    this.acknowledgeMode = acknowledgeMode;
}
 
Example #20
Source File: RabbitProperties.java    From summerframework with Apache License 2.0 4 votes vote down vote up
public AcknowledgeMode getAcknowledgeMode() {
    return this.acknowledgeMode;
}
 
Example #21
Source File: MessageListenerContainerFactory.java    From tutorials with MIT License 3 votes vote down vote up
public MessageListenerContainer createMessageListenerContainer(String queueName) {

        SimpleMessageListenerContainer mlc = new SimpleMessageListenerContainer(connectionFactory);

        mlc.addQueueNames(queueName);
        mlc.setAcknowledgeMode(AcknowledgeMode.AUTO);

        return mlc;
    }