org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer Java Examples

The following examples show how to use org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer. 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: RabbitmqFactory.java    From shine-mq with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化消息监听器容器
 */
private void initMsgListenerAdapter() {
    listenerContainer = new DirectMessageListenerContainer();
    listenerContainer.setConnectionFactory(rabbitConnectionFactory);
    if (rabbit.getAcknowledgeMode() == 1) {
        listenerContainer.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    } else {
        listenerContainer.setAcknowledgeMode(
                rabbit.getAcknowledgeMode() == 2 ? AcknowledgeMode.NONE : AcknowledgeMode.AUTO);
    }
    listenerContainer.setMessageListener(msgAdapterHandler);
    listenerContainer.setErrorHandler(new MessageErrorHandler());
    if (rabbit.getPrefetchCount() != null) {
        listenerContainer.setPrefetchCount(rabbit.getPrefetchCount());
    }
    if (rabbit.getConsumersPerQueue() != null) {
        listenerContainer.setConsumersPerQueue(rabbit.getConsumersPerQueue());
    }
    listenerContainer.setQueues(queues.values().toArray(new Queue[queues.size()]));
    listenerContainer.start();
}
 
Example #2
Source File: RabbitMessageChannelBinder.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 6 votes vote down vote up
private void setDMLCProperties(
		ExtendedConsumerProperties<RabbitConsumerProperties> properties,
		DirectMessageListenerContainer listenerContainer, int concurrency) {

	listenerContainer.setConsumersPerQueue(concurrency);
	if (properties.getExtension().getMaxConcurrency() > concurrency) {
		this.logger
				.warn("maxConcurrency is not supported by the direct container type");
	}
	if (properties.getExtension().getBatchSize() > 1) {
		this.logger.warn("batchSize is not supported by the direct container type");
	}
	if (properties.getExtension().getQueueDeclarationRetries() != null) {
		this.logger.warn(
				"queueDeclarationRetries is not supported by the direct container type");
	}
}
 
Example #3
Source File: RabbitMessageChannelBinder.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Override
protected MessageProducer createConsumerEndpoint(
		ConsumerDestination consumerDestination, String group,
		ExtendedConsumerProperties<RabbitConsumerProperties> properties) {
	Assert.state(!HeaderMode.embeddedHeaders.equals(properties.getHeaderMode()),
			"the RabbitMQ binder does not support embedded headers since RabbitMQ supports headers natively");
	String destination = consumerDestination.getName();
	boolean directContainer = properties.getExtension().getContainerType()
			.equals(ContainerType.DIRECT);
	AbstractMessageListenerContainer listenerContainer = directContainer
			? new DirectMessageListenerContainer(this.connectionFactory)
			: new SimpleMessageListenerContainer(this.connectionFactory);
	listenerContainer
			.setAcknowledgeMode(properties.getExtension().getAcknowledgeMode());
	listenerContainer.setChannelTransacted(properties.getExtension().isTransacted());
	listenerContainer
			.setDefaultRequeueRejected(properties.getExtension().isRequeueRejected());
	int concurrency = properties.getConcurrency();
	concurrency = concurrency > 0 ? concurrency : 1;
	if (directContainer) {
		setDMLCProperties(properties,
				(DirectMessageListenerContainer) listenerContainer, concurrency);
	}
	else {
		setSMLCProperties(properties,
				(SimpleMessageListenerContainer) listenerContainer, concurrency);
	}
	listenerContainer.setPrefetchCount(properties.getExtension().getPrefetch());
	listenerContainer
			.setRecoveryInterval(properties.getExtension().getRecoveryInterval());
	listenerContainer.setTaskExecutor(
			new SimpleAsyncTaskExecutor(consumerDestination.getName() + "-"));
	String[] queues = StringUtils.tokenizeToStringArray(destination, ",", true, true);
	listenerContainer.setQueueNames(queues);
	listenerContainer.setAfterReceivePostProcessors(this.decompressingPostProcessor);
	listenerContainer.setMessagePropertiesConverter(
			RabbitMessageChannelBinder.inboundMessagePropertiesConverter);
	listenerContainer.setExclusive(properties.getExtension().isExclusive());
	listenerContainer
			.setMissingQueuesFatal(properties.getExtension().getMissingQueuesFatal());
	if (properties.getExtension().getFailedDeclarationRetryInterval() != null) {
		listenerContainer.setFailedDeclarationRetryInterval(
				properties.getExtension().getFailedDeclarationRetryInterval());
	}
	if (getApplicationEventPublisher() != null) {
		listenerContainer
				.setApplicationEventPublisher(getApplicationEventPublisher());
	}
	else if (getApplicationContext() != null) {
		listenerContainer.setApplicationEventPublisher(getApplicationContext());
	}
	getContainerCustomizer().configure(listenerContainer,
			consumerDestination.getName(), group);
	if (StringUtils.hasText(properties.getExtension().getConsumerTagPrefix())) {
		final AtomicInteger index = new AtomicInteger();
		listenerContainer.setConsumerTagStrategy(
				q -> properties.getExtension().getConsumerTagPrefix() + "#"
						+ index.getAndIncrement());
	}
	listenerContainer.afterPropertiesSet();

	AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(
			listenerContainer);
	adapter.setBindSourceMessage(true);
	adapter.setBeanFactory(this.getBeanFactory());
	adapter.setBeanName("inbound." + destination);
	DefaultAmqpHeaderMapper mapper = DefaultAmqpHeaderMapper.inboundMapper();
	mapper.setRequestHeaderNames(properties.getExtension().getHeaderPatterns());
	adapter.setHeaderMapper(mapper);
	ErrorInfrastructure errorInfrastructure = registerErrorInfrastructure(
			consumerDestination, group, properties);
	if (properties.getMaxAttempts() > 1) {
		adapter.setRetryTemplate(buildRetryTemplate(properties));
		adapter.setRecoveryCallback(errorInfrastructure.getRecoverer());
	}
	else {
		adapter.setErrorMessageStrategy(errorMessageStrategy);
		adapter.setErrorChannel(errorInfrastructure.getErrorChannel());
	}
	adapter.setMessageConverter(passThoughConverter);
	return adapter;
}