Java Code Examples for org.springframework.cloud.stream.provisioning.ConsumerDestination

The following examples show how to use org.springframework.cloud.stream.provisioning.ConsumerDestination. 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
@Override
protected MessageHandler getPolledConsumerErrorMessageHandler(
		ConsumerDestination destination, String group,
		ExtendedConsumerProperties<RocketMQConsumerProperties> properties) {
	return message -> {
		if (message.getPayload() instanceof MessagingException) {
			AcknowledgmentCallback ack = StaticMessageHeaderAccessor
					.getAcknowledgmentCallback(
							((MessagingException) message.getPayload())
									.getFailedMessage());
			if (ack != null) {
				if (properties.getExtension().shouldRequeue()) {
					ack.acknowledge(Status.REQUEUE);
				}
				else {
					ack.acknowledge(Status.REJECT);
				}
			}
		}
	};
}
 
Example 2
@Override
public ConsumerDestination provisionConsumerDestination(String name, String group,
		ExtendedConsumerProperties<KinesisConsumerProperties> properties)
		throws ProvisioningException {

	if (properties.getExtension().isDynamoDbStreams()) {
		if (logger.isInfoEnabled()) {
			logger.info("Using DynamoDB table in DynamoDB Streams support for inbound: " + name);
		}
		return new KinesisConsumerDestination(name, Collections.emptyList());
	}

	if (logger.isInfoEnabled()) {
		logger.info("Using Kinesis stream for inbound: " + name);
	}

	if (properties.getHeaderMode() == null) {
		properties.setHeaderMode(HeaderMode.embeddedHeaders);
	}

	int shardCount = properties.getInstanceCount() * properties.getConcurrency();

	return new KinesisConsumerDestination(name, createOrUpdate(name, shardCount));
}
 
Example 3
@Test
public void testMultiplexOnPartitionedConsumer() throws Exception {
	final ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
	RabbitTestSupport.RabbitProxy proxy = new RabbitTestSupport.RabbitProxy();
	CachingConnectionFactory cf = new CachingConnectionFactory("localhost",
			proxy.getPort());

	final RabbitExchangeQueueProvisioner rabbitExchangeQueueProvisioner = new RabbitExchangeQueueProvisioner(cf);

	consumerProperties.setMultiplex(true);
	consumerProperties.setPartitioned(true);
	consumerProperties.setInstanceIndexList(Arrays.asList(1, 2, 3));

	final ConsumerDestination consumerDestination = rabbitExchangeQueueProvisioner.provisionConsumerDestination("foo", "boo", consumerProperties);

	final String name = consumerDestination.getName();

	assertThat(name).isEqualTo("foo.boo-1,foo.boo-2,foo.boo-3");
}
 
Example 4
@Test
public void testMultiplexOnPartitionedConsumerWithMultipleDestinations() throws Exception {
	final ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
	RabbitTestSupport.RabbitProxy proxy = new RabbitTestSupport.RabbitProxy();
	CachingConnectionFactory cf = new CachingConnectionFactory("localhost",
			proxy.getPort());

	final RabbitExchangeQueueProvisioner rabbitExchangeQueueProvisioner = new RabbitExchangeQueueProvisioner(cf);

	consumerProperties.setMultiplex(true);
	consumerProperties.setPartitioned(true);
	consumerProperties.setInstanceIndexList(Arrays.asList(1, 2, 3));

	final ConsumerDestination consumerDestination = rabbitExchangeQueueProvisioner.provisionConsumerDestination("foo,qaa", "boo", consumerProperties);

	final String name = consumerDestination.getName();

	assertThat(name).isEqualTo("foo.boo-1,foo.boo-2,foo.boo-3,qaa.boo-1,qaa.boo-2,qaa.boo-3");
}
 
Example 5
@Test
public void testBadRepublishSetting() throws IOException {
	ConnectionFactory cf = mock(ConnectionFactory.class);
	Connection conn = mock(Connection.class);
	given(cf.createConnection()).willReturn(conn);
	Channel channel = mock(Channel.class);
	given(channel.isOpen()).willReturn(true);
	given(channel.exchangeDeclarePassive("DLX")).willThrow(new IOException());
	given(conn.createChannel(false)).willReturn(channel);
	RabbitProperties props = new RabbitProperties();
	RabbitMessageChannelBinder binder = new RabbitMessageChannelBinder(cf, props, null);
	RabbitConsumerProperties extension = new RabbitConsumerProperties();
	ExtendedConsumerProperties<RabbitConsumerProperties> bindingProps =
			new ExtendedConsumerProperties<RabbitConsumerProperties>(extension);
	MessageHandler handler = binder.getErrorMessageHandler(mock(ConsumerDestination.class), "foo", bindingProps);
	ErrorMessage message = new ErrorMessage(new RuntimeException("test"),
			Collections.singletonMap(IntegrationMessageHeaderAccessor.SOURCE_DATA,
					new Message("foo".getBytes(), new MessageProperties())));
	handler.handleMessage(message);
	handler.handleMessage(message);
	verify(channel, times(1)).exchangeDeclarePassive("DLX");
	verify(channel, never()).basicPublish(any(), any(), eq(false), any(), any());
}
 
Example 6
@Override
public ConsumerDestination provisionConsumerDestination(String name, String group,
		ExtendedConsumerProperties<RocketMQConsumerProperties> properties)
		throws ProvisioningException {
	checkTopic(name);
	return new RocketConsumerDestination(name);
}
 
Example 7
@Override
protected PolledConsumerResources createPolledConsumerResources(String name,
		String group, ConsumerDestination destination,
		ExtendedConsumerProperties<RocketMQConsumerProperties> consumerProperties) {
	RocketMQMessageSource rocketMQMessageSource = new RocketMQMessageSource(
			rocketBinderConfigurationProperties, consumerProperties, name, group);
	return new PolledConsumerResources(rocketMQMessageSource,
			registerErrorInfrastructure(destination, group, consumerProperties,
					true));
}
 
Example 8
@Override
protected MessageProducer createConsumerEndpoint(ConsumerDestination destination,
		String group,
		ExtendedConsumerProperties<KinesisConsumerProperties> properties) {

	ConsumerDestination destinationToUse = destination;

	if (properties.getExtension().isDynamoDbStreams()) {
		DescribeTableResult describeTableResult = this.dynamoDBClient.describeTable(destinationToUse.getName());
		String latestStreamArn = describeTableResult.getTable().getLatestStreamArn();
		if (StringUtils.hasText(latestStreamArn)) {
			destinationToUse = new KinesisConsumerDestination(latestStreamArn, Collections.emptyList());
		}
		else {
			throw new ProvisioningException("The DynamoDB table ["
					+ destinationToUse.getName()
					+ "] doesn't have Streams enabled.");
		}
	}
	else {
		this.streamsInUse.add(destinationToUse.getName());
	}

	MessageProducer adapter;
	if (this.configurationProperties.isKplKclEnabled()) {
		adapter = createKclConsumerEndpoint(destinationToUse, group, properties);
	}
	else {
		adapter = createKinesisConsumerEndpoint(destinationToUse, group, properties);
	}

	return adapter;
}
 
Example 9
@Test
void testProvisionConsumerSuccessfulWithExistingStream() {
	AmazonKinesis amazonKinesisMock = mock(AmazonKinesis.class);
	KinesisBinderConfigurationProperties binderProperties = new KinesisBinderConfigurationProperties();
	KinesisStreamProvisioner provisioner = new KinesisStreamProvisioner(
			amazonKinesisMock, binderProperties);

	ExtendedConsumerProperties<KinesisConsumerProperties> extendedConsumerProperties =
			new ExtendedConsumerProperties<>(
			new KinesisConsumerProperties());

	String name = "test-stream";
	String group = "test-group";

	DescribeStreamResult describeStreamResult = describeStreamResultWithShards(
			Collections.singletonList(new Shard()));

	when(amazonKinesisMock.describeStream(any(DescribeStreamRequest.class)))
			.thenReturn(describeStreamResult);

	ConsumerDestination destination = provisioner.provisionConsumerDestination(name,
			group, extendedConsumerProperties);

	verify(amazonKinesisMock).describeStream(any(DescribeStreamRequest.class));

	assertThat(destination.getName()).isEqualTo(name);
}
 
Example 10
@Test
void testProvisionConsumerSuccessfulWithNewStream() {
	AmazonKinesis amazonKinesisMock = mock(AmazonKinesis.class);
	KinesisBinderConfigurationProperties binderProperties = new KinesisBinderConfigurationProperties();
	KinesisStreamProvisioner provisioner = new KinesisStreamProvisioner(
			amazonKinesisMock, binderProperties);
	int instanceCount = 1;
	int concurrency = 1;

	ExtendedConsumerProperties<KinesisConsumerProperties> extendedConsumerProperties =
			new ExtendedConsumerProperties<>(
			new KinesisConsumerProperties());
	extendedConsumerProperties.setInstanceCount(instanceCount);
	extendedConsumerProperties.setConcurrency(concurrency);

	String name = "test-stream";
	String group = "test-group";

	DescribeStreamResult describeStreamResult = describeStreamResultWithShards(
			Collections.singletonList(new Shard()));

	when(amazonKinesisMock.describeStream(any(DescribeStreamRequest.class)))
			.thenThrow(new ResourceNotFoundException("I got nothing"))
			.thenReturn(describeStreamResult);

	when(amazonKinesisMock.createStream(name, instanceCount * concurrency))
			.thenReturn(new CreateStreamResult());

	ConsumerDestination destination = provisioner.provisionConsumerDestination(name,
			group, extendedConsumerProperties);

	verify(amazonKinesisMock, times(2))
			.describeStream(any(DescribeStreamRequest.class));

	verify(amazonKinesisMock).createStream(name, instanceCount * concurrency);

	assertThat(destination.getName()).isEqualTo(name);
}
 
Example 11
@Override
protected MessageProducer createConsumerEndpoint(ConsumerDestination destination,
		String group,
		ExtendedConsumerProperties<KinesisConsumerProperties> properties) {

	MessageProducer messageProducer = super.createConsumerEndpoint(destination,
			group, properties);
	DirectFieldAccessor dfa = new DirectFieldAccessor(messageProducer);
	dfa.setPropertyValue("describeStreamBackoff", 10);
	dfa.setPropertyValue("consumerBackoff", 10);
	dfa.setPropertyValue("idleBetweenPolls", 1);
	return messageProducer;
}
 
Example 12
Source Project: spring-cloud-gcp   Source File: PubSubChannelProvisioner.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ConsumerDestination provisionConsumerDestination(String topicName, String group,
		ExtendedConsumerProperties<PubSubConsumerProperties> properties)
		throws ProvisioningException {

	Topic topic = makeSureTopicExists(topicName, properties.getExtension().isAutoCreateResources());

	String subscriptionName;
	Subscription subscription;
	if (StringUtils.hasText(group)) {
		// Use <topicName>.<group> as subscription name
		subscriptionName = topicName + "." + group;
		subscription = this.pubSubAdmin.getSubscription(subscriptionName);
	}
	else {
		// Generate anonymous random group since one wasn't provided
		subscriptionName = "anonymous." + topicName + "." + UUID.randomUUID().toString();
		subscription = this.pubSubAdmin.createSubscription(subscriptionName, topicName);
		this.anonymousGroupSubscriptionNames.add(subscriptionName);
	}

	// make sure subscription exists
	if (subscription == null) {
		if (properties.getExtension().isAutoCreateResources()) {
			this.pubSubAdmin.createSubscription(subscriptionName, topicName);
		}
		else {
			throw new ProvisioningException("Non-existing '" + subscriptionName + "' subscription.");
		}
	}
	else if (!subscription.getTopic().equals(topic.getName())) {
		throw new ProvisioningException(
				"Existing '" + subscriptionName + "' subscription is for a different topic '"
						+ subscription.getTopic() + "'.");
	}
	return new PubSubConsumerDestination(subscriptionName);
}
 
Example 13
Source Project: spring-cloud-gcp   Source File: PubSubChannelProvisioner.java    License: Apache License 2.0 5 votes vote down vote up
public void afterUnbindConsumer(ConsumerDestination destination) {
	if (this.anonymousGroupSubscriptionNames.remove(destination.getName())) {
		try {
			this.pubSubAdmin.deleteSubscription(destination.getName());
		}
		catch (Exception ex) {
			LOGGER.warn("Failed to delete auto-created anonymous subscription '" + destination.getName() + "'.");
		}
	}
}
 
Example 14
@Override
protected MessageProducer createConsumerEndpoint(ConsumerDestination destination, String group,
		ExtendedConsumerProperties<PubSubConsumerProperties> properties) {

	PubSubInboundChannelAdapter adapter = new PubSubInboundChannelAdapter(this.pubSubTemplate,
			destination.getName());

	ErrorInfrastructure errorInfrastructure = registerErrorInfrastructure(destination, group, properties);
	adapter.setErrorChannel(errorInfrastructure.getErrorChannel());
	adapter.setAckMode(properties.getExtension().getAckMode());

	return adapter;
}
 
Example 15
@Override
public ConsumerDestination provisionConsumerDestination(final String name,
		final String group,
		ExtendedConsumerProperties<KafkaConsumerProperties> properties) {
	if (!properties.isMultiplex()) {
		return doProvisionConsumerDestination(name, group, properties);
	}
	else {
		String[] destinations = StringUtils.commaDelimitedListToStringArray(name);
		for (String destination : destinations) {
			doProvisionConsumerDestination(destination.trim(), group, properties);
		}
		return new KafkaConsumerDestination(name);
	}
}
 
Example 16
private ConsumerDestination createDlqIfNeedBe(AdminClient adminClient, String name,
		String group, ExtendedConsumerProperties<KafkaConsumerProperties> properties,
		boolean anonymous, int partitions) {

	if (properties.getExtension().isEnableDlq() && !anonymous) {
		String dlqTopic = StringUtils.hasText(properties.getExtension().getDlqName())
				? properties.getExtension().getDlqName()
				: "error." + name + "." + group;
		int dlqPartitions = properties.getExtension().getDlqPartitions() == null
				? partitions
				: properties.getExtension().getDlqPartitions();
		try {
			createTopicAndPartitions(adminClient, dlqTopic, dlqPartitions,
					properties.getExtension().isAutoRebalanceEnabled(),
					properties.getExtension().getTopic());
		}
		catch (Throwable throwable) {
			if (throwable instanceof Error) {
				throw (Error) throwable;
			}
			else {
				throw new ProvisioningException("provisioning exception", throwable);
			}
		}
		return new KafkaConsumerDestination(name, partitions, dlqTopic);
	}
	return null;
}
 
Example 17
@Override
protected MessageHandler getPolledConsumerErrorMessageHandler(
		ConsumerDestination destination, String group,
		ExtendedConsumerProperties<KafkaConsumerProperties> properties) {
	if (properties.getExtension().isEnableDlq()) {
		return getErrorMessageHandler(destination, group, properties);
	}
	final MessageHandler superHandler = super.getErrorMessageHandler(destination,
			group, properties);
	return (message) -> {
		ConsumerRecord<?, ?> record = (ConsumerRecord<?, ?>) message.getHeaders()
				.get(KafkaHeaders.RAW_DATA);
		if (!(message instanceof ErrorMessage)) {
			logger.error("Expected an ErrorMessage, not a "
					+ message.getClass().toString() + " for: " + message);
		}
		else if (record == null) {
			if (superHandler != null) {
				superHandler.handleMessage(message);
			}
		}
		else {
			if (message.getPayload() instanceof MessagingException) {
				AcknowledgmentCallback ack = StaticMessageHeaderAccessor
						.getAcknowledgmentCallback(
								((MessagingException) message.getPayload())
										.getFailedMessage());
				if (ack != null) {
					if (isAutoCommitOnError(properties)) {
						ack.acknowledge(AcknowledgmentCallback.Status.REJECT);
					}
					else {
						ack.acknowledge(AcknowledgmentCallback.Status.REQUEUE);
					}
				}
			}
		}
	};
}
 
Example 18
@SuppressWarnings({ "rawtypes", "unchecked" })
KafkaTestBinder(KafkaBinderConfigurationProperties binderConfiguration,
		KafkaTopicProvisioner kafkaTopicProvisioner, DlqPartitionFunction dlqPartitionFunction) {

	try {
		KafkaMessageChannelBinder binder = new KafkaMessageChannelBinder(
				binderConfiguration, kafkaTopicProvisioner, null, null, null, dlqPartitionFunction) {

			/*
			 * Some tests use multiple instance indexes for the same topic; we need to
			 * make the error infrastructure beans unique.
			 */
			@Override
			protected String errorsBaseName(ConsumerDestination destination,
					String group,
					ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties) {
				return super.errorsBaseName(destination, group, consumerProperties)
						+ "-" + consumerProperties.getInstanceIndex();
			}

		};

		ProducerListener producerListener = new LoggingProducerListener();
		binder.setProducerListener(producerListener);
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
				Config.class);
		setApplicationContext(context);
		binder.setApplicationContext(context);
		binder.afterPropertiesSet();
		this.setPollableConsumerBinder(binder);
	}
	catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example 19
@Override
protected PolledConsumerResources createPolledConsumerResources(String name,
		String group, ConsumerDestination destination,
		ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties) {

	Assert.isTrue(!consumerProperties.isMultiplex(),
			"The Spring Integration polled MessageSource does not currently support muiltiple queues");
	AmqpMessageSource source = new AmqpMessageSource(this.connectionFactory,
			destination.getName());
	source.setRawMessageHeader(true);
	getMessageSourceCustomizer().configure(source, destination.getName(), group);
	return new PolledConsumerResources(source, registerErrorInfrastructure(
			destination, group, consumerProperties, true));
}
 
Example 20
@Override
protected MessageHandler getPolledConsumerErrorMessageHandler(
		ConsumerDestination destination, String group,
		ExtendedConsumerProperties<RabbitConsumerProperties> properties) {
	MessageHandler handler = getErrorMessageHandler(destination, group, properties);
	if (handler != null) {
		return handler;
	}
	final MessageHandler superHandler = super.getErrorMessageHandler(destination,
			group, properties);
	return message -> {
		Message amqpMessage = (Message) message.getHeaders()
				.get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE);
		if (!(message instanceof ErrorMessage)) {
			logger.error("Expected an ErrorMessage, not a "
					+ message.getClass().toString() + " for: " + message);
		}
		else if (amqpMessage == null) {
			if (superHandler != null) {
				superHandler.handleMessage(message);
			}
		}
		else {
			if (message.getPayload() instanceof MessagingException) {
				AcknowledgmentCallback ack = StaticMessageHeaderAccessor
						.getAcknowledgmentCallback(
								((MessagingException) message.getPayload())
										.getFailedMessage());
				if (ack != null) {
					if (properties.getExtension().isRequeueRejected()) {
						ack.acknowledge(Status.REQUEUE);
					}
					else {
						ack.acknowledge(Status.REJECT);
					}
				}
			}
		}
	};
}
 
Example 21
@Override
protected void afterUnbindConsumer(ConsumerDestination consumerDestination,
		String group,
		ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties) {
	provisioningProvider.cleanAutoDeclareContext(consumerDestination,
			consumerProperties);
}
 
Example 22
@Override
public ConsumerDestination provisionConsumerDestination(String name, String group,
		ExtendedConsumerProperties<RabbitConsumerProperties> properties) {
	ConsumerDestination consumerDestination;
	if (!properties.isMultiplex()) {
		consumerDestination = doProvisionConsumerDestination(name, group, properties);
	}
	else {
		String[] provisionedDestinations = Stream
				.of(StringUtils.tokenizeToStringArray(name, ",", true, true))
				.flatMap(destination -> {
					if (properties.isPartitioned() && !ObjectUtils.isEmpty(properties.getInstanceIndexList())) {
						List<String> consumerDestinationNames = new ArrayList<>();

						for (Integer index : properties.getInstanceIndexList()) {
							ExtendedConsumerProperties<RabbitConsumerProperties> temporaryProperties =
									new ExtendedConsumerProperties<>(properties.getExtension());
							BeanUtils.copyProperties(properties, temporaryProperties);
							temporaryProperties.setInstanceIndex(index);
							consumerDestinationNames.add(doProvisionConsumerDestination(destination, group,
									temporaryProperties).getName());
						}

						return consumerDestinationNames.stream();
					}
					else {
						return Stream.of(doProvisionConsumerDestination(destination, group,
								properties).getName());
					}
				})
				.toArray(String[]::new);
		consumerDestination = new RabbitConsumerDestination(
				StringUtils.arrayToCommaDelimitedString(provisionedDestinations),
				null);
	}
	return consumerDestination;
}
 
Example 23
public void cleanAutoDeclareContext(ConsumerDestination destination,
		ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties) {
	synchronized (this.autoDeclareContext) {
		Stream.of(StringUtils.tokenizeToStringArray(destination.getName(), ",", true,
				true)).forEach(name -> {
					name = name.trim();
					removeSingleton(name + ".binding");
					removeSingleton(name);
					String dlq = name + ".dlq";
					removeSingleton(dlq + ".binding");
					removeSingleton(dlq);
				});
	}
}
 
Example 24
private void destroyErrorInfrastructure(ConsumerDestination destination, String group,
		C properties) {
	try {
		String recoverer = getErrorRecovererName(destination, group, properties);

		destroyBean(recoverer);

		String errorChannelName = errorsBaseName(destination, group, properties);
		String errorMessageHandlerName = getErrorMessageHandlerName(destination,
				group, properties);
		String errorBridgeHandlerName = getErrorBridgeName(destination, group,
				properties);
		MessageHandler bridgeHandler = null;
		if (getApplicationContext().containsBean(errorBridgeHandlerName)) {
			bridgeHandler = getApplicationContext().getBean(errorBridgeHandlerName,
					MessageHandler.class);
		}
		MessageHandler handler = null;
		if (getApplicationContext().containsBean(errorMessageHandlerName)) {
			handler = getApplicationContext().getBean(errorMessageHandlerName,
					MessageHandler.class);
		}
		if (getApplicationContext().containsBean(errorChannelName)) {
			SubscribableChannel channel = getApplicationContext()
					.getBean(errorChannelName, SubscribableChannel.class);
			if (bridgeHandler != null) {
				channel.unsubscribe(bridgeHandler);
				destroyBean(errorBridgeHandlerName);
			}
			if (handler != null) {
				channel.unsubscribe(handler);
				destroyBean(errorMessageHandlerName);
			}
			destroyBean(errorChannelName);
		}
	}
	catch (IllegalStateException e) {
		// context is shutting down.
	}
}
 
Example 25
Source Project: spring-cloud-stream   Source File: TestChannelBinder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected MessageProducer createConsumerEndpoint(ConsumerDestination destination,
		String group, ConsumerProperties properties) throws Exception {
	ErrorMessageStrategy errorMessageStrategy = new DefaultErrorMessageStrategy();
	SubscribableChannel siBinderInputChannel = ((SpringIntegrationConsumerDestination) destination)
			.getChannel();

	IntegrationMessageListeningContainer messageListenerContainer = new IntegrationMessageListeningContainer();
	IntegrationBinderInboundChannelAdapter adapter = new IntegrationBinderInboundChannelAdapter(
			messageListenerContainer);

	String groupName = StringUtils.hasText(group) ? group : "anonymous";
	ErrorInfrastructure errorInfrastructure = registerErrorInfrastructure(destination,
			groupName, properties);
	if (properties.getMaxAttempts() > 1) {
		adapter.setRetryTemplate(buildRetryTemplate(properties));
		adapter.setRecoveryCallback(errorInfrastructure.getRecoverer());
	}
	else {
		adapter.setErrorMessageStrategy(errorMessageStrategy);
		adapter.setErrorChannel(errorInfrastructure.getErrorChannel());
	}

	siBinderInputChannel.subscribe(messageListenerContainer);

	return adapter;
}
 
Example 26
Source Project: spring-cloud-stream   Source File: TestChannelBinder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected PolledConsumerResources createPolledConsumerResources(String name,
		String group, ConsumerDestination destination,
		ConsumerProperties consumerProperties) {
	return new PolledConsumerResources(this.messageSourceDelegate,
			registerErrorInfrastructure(destination, group, consumerProperties));
}
 
Example 27
Source Project: spring-cloud-stream   Source File: TestChannelBinder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected MessageHandler getErrorMessageHandler(ConsumerDestination destination,
		String group, ConsumerProperties consumerProperties) {
	return m -> {
		this.logger.debug("Error handled: " + m);
		this.lastError = m;
	};
}
 
Example 28
/**
 * Will provision consumer destination as SI {@link DirectChannel}.
 */
@Override
public ConsumerDestination provisionConsumerDestination(String name, String group,
		ConsumerProperties properties) throws ProvisioningException {
	SubscribableChannel destination = this.provisionDestination(name, false);
	if (this.source != null) {
		this.source.setChannel(destination);
	}
	return new SpringIntegrationConsumerDestination(name, destination);
}
 
Example 29
@Override
protected MessageProducer createConsumerEndpoint(ConsumerDestination destination,
		String group,
		ExtendedConsumerProperties<RocketMQConsumerProperties> consumerProperties)
		throws Exception {
	if (group == null || "".equals(group)) {
		throw new RuntimeException(
				"'group must be configured for channel " + destination.getName());
	}

	RocketMQListenerBindingContainer listenerContainer = new RocketMQListenerBindingContainer(
			consumerProperties, rocketBinderConfigurationProperties, this);
	listenerContainer.setConsumerGroup(group);
	listenerContainer.setTopic(destination.getName());
	listenerContainer.setConsumeThreadMax(consumerProperties.getConcurrency());
	listenerContainer.setSuspendCurrentQueueTimeMillis(
			consumerProperties.getExtension().getSuspendCurrentQueueTimeMillis());
	listenerContainer.setDelayLevelWhenNextConsume(
			consumerProperties.getExtension().getDelayLevelWhenNextConsume());
	listenerContainer
			.setNameServer(rocketBinderConfigurationProperties.getNameServer());
	listenerContainer.setHeaderMapper(createHeaderMapper(consumerProperties));

	RocketMQInboundChannelAdapter rocketInboundChannelAdapter = new RocketMQInboundChannelAdapter(
			listenerContainer, consumerProperties, instrumentationManager);

	topicInUse.put(destination.getName(), group);

	ErrorInfrastructure errorInfrastructure = registerErrorInfrastructure(destination,
			group, consumerProperties);
	if (consumerProperties.getMaxAttempts() > 1) {
		rocketInboundChannelAdapter
				.setRetryTemplate(buildRetryTemplate(consumerProperties));
		rocketInboundChannelAdapter
				.setRecoveryCallback(errorInfrastructure.getRecoverer());
	}
	else {
		rocketInboundChannelAdapter
				.setErrorChannel(errorInfrastructure.getErrorChannel());
	}

	return rocketInboundChannelAdapter;
}
 
Example 30
private MessageProducer createKclConsumerEndpoint(ConsumerDestination destination, String group,
		ExtendedConsumerProperties<KinesisConsumerProperties> properties) {
	KinesisConsumerProperties kinesisConsumerProperties = properties.getExtension();

	String shardIteratorType = kinesisConsumerProperties.getShardIteratorType();

	AmazonKinesis amazonKinesisClient =
			kinesisConsumerProperties.isDynamoDbStreams()
					? this.dynamoDBStreamsAdapter
					: this.amazonKinesis;

	String stream = destination.getName();

	KinesisClientLibConfiguration kinesisClientLibConfiguration = obtainKinesisClientLibConfiguration(stream, group);

	KclMessageDrivenChannelAdapter adapter;

	String consumerGroup;
	if (kinesisClientLibConfiguration == null) {
		adapter = new KclMessageDrivenChannelAdapter(stream, amazonKinesisClient, this.cloudWatchClient,
						this.dynamoDBClient, this.awsCredentialsProvider);
		boolean anonymous = !StringUtils.hasText(group);
		consumerGroup = anonymous ? "anonymous." + UUID.randomUUID().toString() : group;
		adapter.setConsumerGroup(consumerGroup);
		if (StringUtils.hasText(shardIteratorType)) {
			adapter.setStreamInitialSequence(InitialPositionInStream.valueOf(shardIteratorType));
		}
		adapter.setIdleBetweenPolls(kinesisConsumerProperties.getIdleBetweenPolls());
		adapter.setConsumerBackoff(kinesisConsumerProperties.getConsumerBackoff());
		if (kinesisConsumerProperties.getWorkerId() != null) {
			adapter.setWorkerId(kinesisConsumerProperties.getWorkerId());
		}
	}
	else {
		adapter = new KclMessageDrivenChannelAdapter(kinesisClientLibConfiguration, amazonKinesisClient,
				this.cloudWatchClient, this.dynamoDBClient);
		consumerGroup = kinesisClientLibConfiguration.getApplicationName();
	}

	adapter.setCheckpointMode(kinesisConsumerProperties.getCheckpointMode());
	adapter.setCheckpointsInterval(kinesisConsumerProperties.getCheckpointInterval());

	adapter.setListenerMode(kinesisConsumerProperties.getListenerMode());

	if (properties.isUseNativeDecoding()) {
		adapter.setConverter(null);
	}
	else {
		// Defer byte[] conversion to the InboundContentTypeConvertingInterceptor
		adapter.setConverter((bytes) -> bytes);
	}

	ErrorInfrastructure errorInfrastructure = registerErrorInfrastructure(destination, consumerGroup, properties);
	adapter.setErrorMessageStrategy(ERROR_MESSAGE_STRATEGY);
	adapter.setErrorChannel(errorInfrastructure.getErrorChannel());
	adapter.setBindSourceRecord(true);
	return adapter;
}