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

The following examples show how to use org.springframework.cloud.stream.provisioning.ConsumerDestination. 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: RocketMQMessageChannelBinder.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@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
Source File: KinesisStreamProvisioner.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 6 votes vote down vote up
@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
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 6 votes vote down vote up
@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
Source File: RepublishUnitTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: RabbitExchangeQueueProvisioner.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@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 #7
Source File: KafkaTopicProvisioner.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
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 #8
Source File: KafkaMessageChannelBinder.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@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 #9
Source File: KafkaTestBinder.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@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 #10
Source File: RabbitMessageChannelBinder.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@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 #11
Source File: RocketMQTopicProvisioner.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public ConsumerDestination provisionConsumerDestination(String name, String group,
		ExtendedConsumerProperties<RocketMQConsumerProperties> properties)
		throws ProvisioningException {
	checkTopic(name);
	return new RocketConsumerDestination(name);
}
 
Example #12
Source File: RabbitMessageChannelBinder.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@Override
protected void afterUnbindConsumer(ConsumerDestination consumerDestination,
		String group,
		ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties) {
	provisioningProvider.cleanAutoDeclareContext(consumerDestination,
			consumerProperties);
}
 
Example #13
Source File: RabbitMessageChannelBinder.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@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 #14
Source File: RabbitExchangeQueueProvisioner.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
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 #15
Source File: AbstractMessageChannelBinder.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
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 #16
Source File: TestChannelBinder.java    From spring-cloud-stream with 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 #17
Source File: TestChannelBinder.java    From spring-cloud-stream with 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 #18
Source File: TestChannelBinder.java    From spring-cloud-stream with 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 #19
Source File: TestChannelBinderProvisioner.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #20
Source File: KafkaTopicProvisioner.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@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 #21
Source File: RocketMQMessageChannelBinder.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@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 #22
Source File: KinesisMessageChannelBinder.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 5 votes vote down vote up
@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 #23
Source File: PubSubMessageChannelBinder.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@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 #24
Source File: PubSubChannelProvisioner.java    From spring-cloud-gcp with 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 #25
Source File: PubSubChannelProvisioner.java    From spring-cloud-gcp with 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 #26
Source File: KinesisTestBinder.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 5 votes vote down vote up
@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 #27
Source File: KinesisStreamProvisionerTests.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 5 votes vote down vote up
@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 #28
Source File: KinesisStreamProvisionerTests.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 5 votes vote down vote up
@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 #29
Source File: RocketMQMessageChannelBinder.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@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
Source File: KafkaTopicProvisioner.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
private ConsumerDestination doProvisionConsumerDestination(final String name,
		final String group,
		ExtendedConsumerProperties<KafkaConsumerProperties> properties) {

	if (properties.getExtension().isDestinationIsPattern()) {
		Assert.isTrue(!properties.getExtension().isEnableDlq(),
				"enableDLQ is not allowed when listening to topic patterns");
		if (logger.isDebugEnabled()) {
			logger.debug("Listening to a topic pattern - " + name
					+ " - no provisioning performed");
		}
		return new KafkaConsumerDestination(name);
	}
	KafkaTopicUtils.validateTopicName(name);
	boolean anonymous = !StringUtils.hasText(group);
	Assert.isTrue(!anonymous || !properties.getExtension().isEnableDlq(),
			"DLQ support is not available for anonymous subscriptions");
	if (properties.getInstanceCount() == 0) {
		throw new IllegalArgumentException("Instance count cannot be zero");
	}
	int partitionCount = properties.getInstanceCount() * properties.getConcurrency();
	ConsumerDestination consumerDestination = new KafkaConsumerDestination(name);
	try (AdminClient adminClient = createAdminClient()) {
		createTopic(adminClient, name, partitionCount,
				properties.getExtension().isAutoRebalanceEnabled(),
				properties.getExtension().getTopic());
		if (this.configurationProperties.isAutoCreateTopics()) {
			DescribeTopicsResult describeTopicsResult = adminClient
					.describeTopics(Collections.singletonList(name));
			KafkaFuture<Map<String, TopicDescription>> all = describeTopicsResult
					.all();
			try {
				Map<String, TopicDescription> topicDescriptions = all
						.get(this.operationTimeout, TimeUnit.SECONDS);
				TopicDescription topicDescription = topicDescriptions.get(name);
				int partitions = topicDescription.partitions().size();
				consumerDestination = createDlqIfNeedBe(adminClient, name, group,
						properties, anonymous, partitions);
				if (consumerDestination == null) {
					consumerDestination = new KafkaConsumerDestination(name,
							partitions);
				}
			}
			catch (Exception ex) {
				throw new ProvisioningException("provisioning exception", ex);
			}
		}
	}
	return consumerDestination;
}