Java Code Examples for org.springframework.cloud.stream.binder.ExtendedConsumerProperties

The following examples show how to use org.springframework.cloud.stream.binder.ExtendedConsumerProperties. 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
private void setSMLCProperties(
		ExtendedConsumerProperties<RabbitConsumerProperties> properties,
		SimpleMessageListenerContainer listenerContainer, int concurrency) {

	listenerContainer.setConcurrentConsumers(concurrency);
	int maxConcurrency = properties.getExtension().getMaxConcurrency();
	if (maxConcurrency > concurrency) {
		listenerContainer.setMaxConcurrentConsumers(maxConcurrency);
	}
	listenerContainer.setDeBatchingEnabled(!properties.isBatchMode());
	listenerContainer.setBatchSize(properties.getExtension().getBatchSize());
	if (properties.getExtension().getQueueDeclarationRetries() != null) {
		listenerContainer.setDeclarationRetries(
				properties.getExtension().getQueueDeclarationRetries());
	}
}
 
Example 2
@Test
public void testAnonWithBuiltInExchangeCustomPrefix() throws Exception {
	RabbitTestBinder binder = getBinder();
	ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
	properties.getExtension().setDeclareExchange(false);
	properties.getExtension().setQueueNameGroupOnly(true);
	properties.getExtension().setAnonymousGroupPrefix("customPrefix.");

	Binding<MessageChannel> consumerBinding = binder.bindConsumer("amq.topic", null,
			createBindableChannel("input", new BindingProperties()), properties);
	Lifecycle endpoint = extractEndpoint(consumerBinding);
	SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint,
			"messageListenerContainer", SimpleMessageListenerContainer.class);
	String queueName = container.getQueueNames()[0];
	assertThat(queueName).startsWith("customPrefix.");
	assertThat(container.isRunning()).isTrue();
	consumerBinding.unbind();
	assertThat(container.isRunning()).isFalse();
}
 
Example 3
@Override
protected Binding<KStream<Object, Object>> doBindConsumer(String name, String group,
		KStream<Object, Object> inputTarget,
		ExtendedConsumerProperties<KafkaStreamsConsumerProperties> properties) {

	KStream<Object, Object> delegate = ((KStreamBoundElementFactory.KStreamWrapperHandler)
			((Advised) inputTarget).getAdvisors()[0].getAdvice()).getDelegate();

	this.kafkaStreamsBindingInformationCatalogue.registerConsumerProperties(delegate, properties.getExtension());

	if (!StringUtils.hasText(group)) {
		group = properties.getExtension().getApplicationId();
	}
	KafkaStreamsBinderUtils.prepareConsumerBinding(name, group,
			getApplicationContext(), this.kafkaTopicProvisioner,
			this.binderConfigurationProperties, properties);

	return new DefaultBinding<>(name, group, inputTarget, null);
}
 
Example 4
/**
 * Configure a {@link BackOff} for the after rollback processor, based on the consumer
 * retry properties. If retry is disabled, return a {@link BackOff} that disables
 * retry. Otherwise calculate the {@link ExponentialBackOff#setMaxElapsedTime(long)}
 * so that the {@link BackOff} stops after the configured
 * {@link ExtendedConsumerProperties#getMaxAttempts()}.
 * @param extendedConsumerProperties the properties.
 * @return the backoff.
 */
private BackOff createBackOff(
		final ExtendedConsumerProperties<KafkaConsumerProperties> extendedConsumerProperties) {

	int maxAttempts = extendedConsumerProperties.getMaxAttempts();
	if (maxAttempts < 2) {
		return new FixedBackOff(0L, 0L);
	}
	int initialInterval = extendedConsumerProperties.getBackOffInitialInterval();
	double multiplier = extendedConsumerProperties.getBackOffMultiplier();
	int maxInterval = extendedConsumerProperties.getBackOffMaxInterval();
	ExponentialBackOff backOff = new ExponentialBackOff(initialInterval, multiplier);
	backOff.setMaxInterval(maxInterval);
	long maxElapsed = extendedConsumerProperties.getBackOffInitialInterval();
	double accum = maxElapsed;
	for (int i = 1; i < maxAttempts - 1; i++) {
		accum = accum * multiplier;
		if (accum > maxInterval) {
			accum = maxInterval;
		}
		maxElapsed += accum;
	}
	backOff.setMaxElapsedTime(maxElapsed);
	return backOff;
}
 
Example 5
private TopicPartitionOffset[] getTopicPartitionOffsets(
		Collection<PartitionInfo> listenedPartitions,
		ExtendedConsumerProperties<KafkaConsumerProperties> extendedConsumerProperties,
		ConsumerFactory<?, ?> consumerFactory) {

	final TopicPartitionOffset[] TopicPartitionOffsets =
			new TopicPartitionOffset[listenedPartitions.size()];
	int i = 0;
	SeekPosition seekPosition = null;
	Object resetTo = checkReset(extendedConsumerProperties.getExtension().isResetOffsets(),
			consumerFactory.getConfigurationProperties().get(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG));
	if (resetTo != null) {
		seekPosition = "earliest".equals(resetTo) ? SeekPosition.BEGINNING : SeekPosition.END;
	}
	for (PartitionInfo partition : listenedPartitions) {

		TopicPartitionOffsets[i++] = new TopicPartitionOffset(
				partition.topic(), partition.partition(), seekPosition);
	}
	return TopicPartitionOffsets;
}
 
Example 6
@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 7
@Test
@SuppressWarnings("unchecked")
public void testPartitionCountIncreasedIfAutoAddPartitionsSet() throws Throwable {
	KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties();

	String testTopicName = "existing" + System.currentTimeMillis();
	configurationProperties.setMinPartitionCount(6);
	configurationProperties.setAutoAddPartitions(true);
	Binder binder = getBinder(configurationProperties);
	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	DirectChannel input = createBindableChannel("input",
			createConsumerBindingProperties(consumerProperties));

	Binding<?> binding = binder.bindConsumer(testTopicName, "test", input,
			consumerProperties);
	binding.unbind();
	assertThat(invokePartitionSize(testTopicName)).isEqualTo(6);
}
 
Example 8
@Test
@SuppressWarnings("unchecked")
public void testAutoConfigureTopicsDisabledSucceedsIfTopicExisting()
		throws Throwable {
	KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties();

	String testTopicName = "existing" + System.currentTimeMillis();
	invokeCreateTopic(testTopicName, 5, 1);
	configurationProperties.setAutoCreateTopics(false);
	Binder binder = getBinder(configurationProperties);

	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();

	DirectChannel input = createBindableChannel("input",
			createConsumerBindingProperties(consumerProperties));
	Binding<MessageChannel> binding = binder.bindConsumer(testTopicName, "test",
			input, consumerProperties);
	binding.unbind();
}
 
Example 9
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 10
@Test
@SuppressWarnings("unchecked")
public void testAutoAddPartitionsDisabledSucceedsIfTopicUnderPartitionedAndAutoRebalanceEnabled()
		throws Throwable {
	KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties();

	String testTopicName = "existing" + System.currentTimeMillis();
	invokeCreateTopic(testTopicName, 1, 1);
	configurationProperties.setAutoAddPartitions(false);
	Binder binder = getBinder(configurationProperties);
	GenericApplicationContext context = new GenericApplicationContext();
	context.refresh();

	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();

	DirectChannel input = createBindableChannel("input",
			createConsumerBindingProperties(consumerProperties));

	// this consumer must consume from partition 2
	consumerProperties.setInstanceCount(3);
	consumerProperties.setInstanceIndex(2);
	Binding binding = binder.bindConsumer(testTopicName, "test", input,
			consumerProperties);
	binding.unbind();
	assertThat(invokePartitionSize(testTopicName)).isEqualTo(1);
}
 
Example 11
@Test
public void testNoBrokerOverride() throws Exception {
	Binder binder = getBinder();
	ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties();
	producerProperties.getExtension().getConfiguration().put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "foo");
	BindingProperties outputBindingProperties = createProducerBindingProperties(producerProperties);
	DirectChannel moduleOutputChannel = createBindableChannel("output", outputBindingProperties);
	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	consumerProperties.getExtension().getConfiguration().put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "foo");
	BindingProperties consumerBindingProperties = createConsumerBindingProperties(consumerProperties);
	DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties);

	assertThatExceptionOfType(BinderException.class).isThrownBy(() -> binder.bindProducer("foo.bar",
				moduleOutputChannel, outputBindingProperties.getProducer()))
			.withCauseExactlyInstanceOf(IllegalStateException.class);
	assertThatExceptionOfType(BinderException.class).isThrownBy(() -> binder.bindConsumer("foo.bar",
				"testSendAndReceive", moduleInputChannel, consumerProperties))
			.withCauseExactlyInstanceOf(IllegalStateException.class);
}
 
Example 12
@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 13
@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 14
public RocketMQInboundChannelAdapter(
		RocketMQListenerBindingContainer rocketMQListenerContainer,
		ExtendedConsumerProperties<RocketMQConsumerProperties> consumerProperties,
		InstrumentationManager instrumentationManager) {
	this.rocketMQListenerContainer = rocketMQListenerContainer;
	this.consumerProperties = consumerProperties;
	this.instrumentationManager = instrumentationManager;
}
 
Example 15
@Override
public ConsumerDestination provisionConsumerDestination(String name, String group,
		ExtendedConsumerProperties<RocketMQConsumerProperties> properties)
		throws ProvisioningException {
	checkTopic(name);
	return new RocketConsumerDestination(name);
}
 
Example 16
@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 17
@Test
public void testNonDurablePubSubWithAutoBindDLQ() throws Exception {
	RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());

	RabbitTestBinder binder = getBinder();
	ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
	consumerProperties.getExtension().setPrefix(TEST_PREFIX);
	consumerProperties.getExtension().setAutoBindDlq(true);
	consumerProperties.getExtension().setDurableSubscription(false);
	consumerProperties.setMaxAttempts(1); // disable retry
	BindingProperties bindingProperties = createConsumerBindingProperties(
			consumerProperties);
	DirectChannel moduleInputChannel = createBindableChannel("input",
			bindingProperties);
	moduleInputChannel.setBeanName("nondurabletest");
	moduleInputChannel.subscribe(new MessageHandler() {

		@Override
		public void handleMessage(Message<?> message) throws MessagingException {
			throw new RuntimeException("foo");
		}

	});
	Binding<MessageChannel> consumerBinding = binder.bindConsumer("nondurabletest.0",
			"tgroup", moduleInputChannel, consumerProperties);

	consumerBinding.unbind();
	assertThat(admin.getQueueProperties(TEST_PREFIX + "nondurabletest.0.dlq"))
			.isNull();
}
 
Example 18
@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 19
@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 20
@Test
void testProvisionConsumerExistingStreamUpdateShards() {
	AmazonKinesis amazonKinesisMock = mock(AmazonKinesis.class);
	ArgumentCaptor<UpdateShardCountRequest> updateShardCaptor = ArgumentCaptor
			.forClass(UpdateShardCountRequest.class);
	String name = "test-stream";
	String group = "test-group";
	int targetShardCount = 2;
	KinesisBinderConfigurationProperties binderProperties = new KinesisBinderConfigurationProperties();
	binderProperties.setMinShardCount(targetShardCount);
	binderProperties.setAutoAddShards(true);
	KinesisStreamProvisioner provisioner = new KinesisStreamProvisioner(
			amazonKinesisMock, binderProperties);

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

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

	DescribeStreamResult describeUpdatedStream = describeStreamResultWithShards(
			Arrays.asList(new Shard(), new Shard()));

	when(amazonKinesisMock.describeStream(any(DescribeStreamRequest.class)))
			.thenReturn(describeOriginalStream).thenReturn(describeUpdatedStream);

	provisioner.provisionConsumerDestination(name, group, extendedConsumerProperties);

	verify(amazonKinesisMock, times(1)).updateShardCount(updateShardCaptor.capture());

	assertThat(updateShardCaptor.getValue().getStreamName()).isEqualTo(name);
	assertThat(updateShardCaptor.getValue().getScalingType())
			.isEqualTo(ScalingType.UNIFORM_SCALING.name());
	assertThat(updateShardCaptor.getValue().getTargetShardCount())
			.isEqualTo(targetShardCount);
}
 
Example 21
private Binding declareConsumerBindings(String name, String routingKey,
		ExtendedConsumerProperties<RabbitConsumerProperties> properties,
		Exchange exchange, boolean partitioned, Queue queue) {

	if (partitioned) {
		return partitionedBinding(name, exchange, queue, routingKey, properties.getExtension(),
				properties.getInstanceIndex());
	}
	else {
		return notPartitionedBinding(exchange, queue, routingKey, properties.getExtension());
	}
}
 
Example 22
@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 23
@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 24
@Test
public void testAutoCreateStreamForNonExistingStream() throws Exception {
	KinesisTestBinder binder = getBinder();
	DirectChannel output = createBindableChannel("output", new BindingProperties());
	ExtendedConsumerProperties<KinesisConsumerProperties> consumerProperties = createConsumerProperties();
	Date testDate = new Date();
	consumerProperties.getExtension().setShardIteratorType(
			ShardIteratorType.AT_TIMESTAMP.name() + ":" + testDate.getTime());
	String testStreamName = "nonexisting" + System.currentTimeMillis();
	Binding<?> binding = binder.bindConsumer(testStreamName, "test", output,
			consumerProperties);
	binding.unbind();

	DescribeStreamResult streamResult = AMAZON_KINESIS.describeStream(testStreamName);
	String createdStreamName = streamResult.getStreamDescription().getStreamName();
	int createdShards = streamResult.getStreamDescription().getShards().size();
	String createdStreamStatus = streamResult.getStreamDescription()
			.getStreamStatus();

	assertThat(createdStreamName).isEqualTo(testStreamName);
	assertThat(createdShards).isEqualTo(consumerProperties.getInstanceCount()
			* consumerProperties.getConcurrency());
	assertThat(createdStreamStatus).isEqualTo(StreamStatus.ACTIVE.toString());

	KinesisShardOffset shardOffset = TestUtils.getPropertyValue(binding,
			"lifecycle.streamInitialSequence", KinesisShardOffset.class);
	assertThat(shardOffset.getIteratorType())
			.isEqualTo(ShardIteratorType.AT_TIMESTAMP);
	assertThat(shardOffset.getTimestamp()).isEqualTo(testDate);
}
 
Example 25
@Override
public Binding<MessageChannel> bindConsumer(String name, String group,
		MessageChannel moduleInputChannel,
		ExtendedConsumerProperties<RabbitConsumerProperties> properties) {
	captureConsumerResources(name, group, properties);
	return super.bindConsumer(name, group, moduleInputChannel, properties);
}
 
Example 26
@Test
@SuppressWarnings("unchecked")
public void testBatchListener() throws Exception {
	KinesisTestBinder binder = getBinder();
	ExtendedProducerProperties<KinesisProducerProperties> producerProperties = createProducerProperties();
	DirectChannel output = createBindableChannel("output",
			createProducerBindingProperties(producerProperties));

	Binding<MessageChannel> outputBinding = binder.bindProducer("testBatchListener",
			output, producerProperties);

	for (int i = 0; i < 3; i++) {
		output.send(new GenericMessage<>(i));
	}

	ExtendedConsumerProperties<KinesisConsumerProperties> consumerProperties = createConsumerProperties();
	consumerProperties.getExtension().setListenerMode(ListenerMode.batch);
	consumerProperties.setUseNativeDecoding(true);

	QueueChannel input = new QueueChannel();
	Binding<MessageChannel> inputBinding = binder.bindConsumer("testBatchListener",
			null, input, consumerProperties);

	Message<List<?>> receivedMessage = (Message<List<?>>) receive(input);
	assertThat(receivedMessage).isNotNull();
	assertThat(receivedMessage.getPayload().size()).isEqualTo(3);

	receivedMessage.getPayload().forEach((r) -> {
		assertThat(r).isInstanceOf(Record.class);
	});

	outputBinding.unbind();
	inputBinding.unbind();
}
 
Example 27
@Override
protected ExtendedConsumerProperties<KinesisConsumerProperties> createConsumerProperties() {
	ExtendedConsumerProperties<KinesisConsumerProperties> kinesisConsumerProperties = new ExtendedConsumerProperties<>(
			new KinesisConsumerProperties());
	// set the default values that would normally be propagated by Spring Cloud Stream
	kinesisConsumerProperties.setInstanceCount(1);
	kinesisConsumerProperties.setInstanceIndex(0);
	kinesisConsumerProperties.getExtension().setShardIteratorType(ShardIteratorType.TRIM_HORIZON.name());
	return kinesisConsumerProperties;
}
 
Example 28
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 29
@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 30
@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));
}