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

The following examples show how to use org.springframework.cloud.stream.provisioning.ProducerDestination. 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: KinesisStreamProvisioner.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 6 votes vote down vote up
@Override
public ProducerDestination provisionProducerDestination(String name,
		ExtendedProducerProperties<KinesisProducerProperties> properties)
		throws ProvisioningException {

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

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

	return new KinesisProducerDestination(name,
			createOrUpdate(name, properties.getPartitionCount()));
}
 
Example #2
Source File: KinesisMessageChannelBinder.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 6 votes vote down vote up
@Override
protected MessageHandler createProducerMessageHandler(ProducerDestination destination,
		ExtendedProducerProperties<KinesisProducerProperties> producerProperties,
		MessageChannel errorChannel) {

	FunctionExpression<Message<?>> partitionKeyExpression =
			new FunctionExpression<>((m) ->
					m.getHeaders().containsKey(BinderHeaders.PARTITION_HEADER)
							? m.getHeaders().get(BinderHeaders.PARTITION_HEADER)
							: m.getPayload().hashCode());
	final AbstractAwsMessageHandler<?> messageHandler;
	if (this.configurationProperties.isKplKclEnabled()) {
		messageHandler = createKplMessageHandler(destination, partitionKeyExpression);
	}
	else {
		messageHandler = createKinesisMessageHandler(destination, partitionKeyExpression);
	}
	messageHandler.setSync(producerProperties.getExtension().isSync());
	messageHandler.setSendTimeout(producerProperties.getExtension().getSendTimeout());
	messageHandler.setFailureChannel(errorChannel);
	messageHandler.setBeanFactory(getBeanFactory());

	this.streamsInUse.add(destination.getName());

	return messageHandler;
}
 
Example #3
Source File: AbstractMessageChannelBinder.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
private void destroyErrorInfrastructure(ProducerDestination destination) {
	String errorChannelName = errorsBaseName(destination);
	String errorBridgeHandlerName = getErrorBridgeName(destination);
	MessageHandler bridgeHandler = null;
	if (getApplicationContext().containsBean(errorBridgeHandlerName)) {
		bridgeHandler = getApplicationContext().getBean(errorBridgeHandlerName,
				MessageHandler.class);
	}
	if (getApplicationContext().containsBean(errorChannelName)) {
		SubscribableChannel channel = getApplicationContext()
				.getBean(errorChannelName, SubscribableChannel.class);
		if (bridgeHandler != null) {
			channel.unsubscribe(bridgeHandler);
			((DefaultSingletonBeanRegistry) getApplicationContext().getBeanFactory())
					.destroySingleton(errorBridgeHandlerName);
		}
		((DefaultSingletonBeanRegistry) getApplicationContext().getBeanFactory())
				.destroySingleton(errorChannelName);
	}
}
 
Example #4
Source File: KinesisStreamProvisionerTests.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 6 votes vote down vote up
@Test
void testProvisionProducerSuccessfulWithExistingStream() {
	AmazonKinesis amazonKinesisMock = mock(AmazonKinesis.class);
	KinesisBinderConfigurationProperties binderProperties = new KinesisBinderConfigurationProperties();
	KinesisStreamProvisioner provisioner = new KinesisStreamProvisioner(
			amazonKinesisMock, binderProperties);
	ExtendedProducerProperties<KinesisProducerProperties> extendedProducerProperties =
			new ExtendedProducerProperties<>(new KinesisProducerProperties());
	String name = "test-stream";

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

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

	ProducerDestination destination = provisioner.provisionProducerDestination(name,
			extendedProducerProperties);

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

	assertThat(destination.getName()).isEqualTo(name);
}
 
Example #5
Source File: KafkaTopicProvisioner.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@Override
public ProducerDestination provisionProducerDestination(final String name,
		ExtendedProducerProperties<KafkaProducerProperties> properties) {

	if (logger.isInfoEnabled()) {
		logger.info("Using kafka topic for outbound: " + name);
	}
	KafkaTopicUtils.validateTopicName(name);
	try (AdminClient adminClient = AdminClient.create(this.adminClientProperties)) {
		createTopic(adminClient, name, properties.getPartitionCount(), false,
				properties.getExtension().getTopic());
		int partitions = 0;
		Map<String, TopicDescription> topicDescriptions = new HashMap<>();
		if (this.configurationProperties.isAutoCreateTopics()) {
			this.metadataRetryOperations.execute(context -> {
				try {
					if (logger.isDebugEnabled()) {
						logger.debug("Attempting to retrieve the description for the topic: " + name);
					}
					DescribeTopicsResult describeTopicsResult = adminClient
							.describeTopics(Collections.singletonList(name));
					KafkaFuture<Map<String, TopicDescription>> all = describeTopicsResult
							.all();
					topicDescriptions.putAll(all.get(this.operationTimeout, TimeUnit.SECONDS));
				}
				catch (Exception ex) {
					throw new ProvisioningException("Problems encountered with partitions finding", ex);
				}
				return null;
			});
		}
		TopicDescription topicDescription = topicDescriptions.get(name);
		if (topicDescription != null) {
			partitions = topicDescription.partitions().size();
		}
		return new KafkaProducerDestination(name, partitions);
	}
}
 
Example #6
Source File: TestChannelBinderProvisioner.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
/**
 * Will provision producer destination as an SI {@link PublishSubscribeChannel}. <br>
 * This provides convenience of registering additional subscriber (handler in the test
 * method) along side of being able to call {@link OutputDestination#receive()} to get
 * a {@link Message} for additional assertions.
 */
@Override
public ProducerDestination provisionProducerDestination(String name,
		ProducerProperties properties) throws ProvisioningException {
	SubscribableChannel destination = this.provisionDestination(name, true);
	this.target.setChannel(destination);
	return new SpringIntegrationProducerDestination(name, destination);
}
 
Example #7
Source File: TestChannelBinder.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Override
protected MessageHandler createProducerMessageHandler(ProducerDestination destination,
		ProducerProperties producerProperties, MessageChannel errorChannel)
		throws Exception {
	BridgeHandler handler = new BridgeHandler();
	handler.setBeanFactory(this.beanFactory);
	handler.setOutputChannel(
			((SpringIntegrationProducerDestination) destination).getChannel());
	return handler;
}
 
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 createProducerMessageHandler(
		final ProducerDestination destination,
		ExtendedProducerProperties<KafkaProducerProperties> producerProperties,
		MessageChannel errorChannel) throws Exception {
	throw new IllegalStateException(
			"The abstract binder should not call this method");
}
 
Example #9
Source File: RocketMQTopicProvisioner.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public ProducerDestination provisionProducerDestination(String name,
		ExtendedProducerProperties<RocketMQProducerProperties> properties)
		throws ProvisioningException {
	checkTopic(name);
	return new RocketProducerDestination(name);
}
 
Example #10
Source File: PubSubMessageChannelBinder.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
protected MessageHandler createProducerMessageHandler(ProducerDestination destination,
		ExtendedProducerProperties<PubSubProducerProperties> producerProperties,
		MessageChannel errorChannel) {

	PubSubMessageHandler messageHandler = new PubSubMessageHandler(this.pubSubTemplate, destination.getName());
	messageHandler.setBeanFactory(getBeanFactory());
	return messageHandler;
}
 
Example #11
Source File: PubSubChannelProvisioner.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
public ProducerDestination provisionProducerDestination(String topic,
		ExtendedProducerProperties<PubSubProducerProperties> properties)
		throws ProvisioningException {
	makeSureTopicExists(topic, properties.getExtension().isAutoCreateResources());

	return new PubSubProducerDestination(topic);
}
 
Example #12
Source File: KinesisStreamProvisionerTests.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 5 votes vote down vote up
@Test
void testProvisionProducerSuccessfulWithNewStream() {
	AmazonKinesis amazonKinesisMock = mock(AmazonKinesis.class);
	KinesisBinderConfigurationProperties binderProperties = new KinesisBinderConfigurationProperties();
	KinesisStreamProvisioner provisioner = new KinesisStreamProvisioner(
			amazonKinesisMock, binderProperties);
	ExtendedProducerProperties<KinesisProducerProperties> extendedProducerProperties =
			new ExtendedProducerProperties<>(
			new KinesisProducerProperties());

	String name = "test-stream";
	Integer shards = 1;

	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, shards))
			.thenReturn(new CreateStreamResult());

	ProducerDestination destination = provisioner.provisionProducerDestination(name,
			extendedProducerProperties);

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

	verify(amazonKinesisMock).createStream(name, shards);

	assertThat(destination.getName()).isEqualTo(name);
}
 
Example #13
Source File: KinesisMessageChannelBinder.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 5 votes vote down vote up
private AbstractAwsMessageHandler<?> createKplMessageHandler(ProducerDestination destination,
		FunctionExpression<Message<?>> partitionKeyExpression) {

	final KplMessageHandler messageHandler;
	messageHandler = new KplMessageHandler(new KinesisProducer(this.kinesisProducerConfiguration));
	messageHandler.setStream(destination.getName());
	messageHandler.setPartitionKeyExpression(partitionKeyExpression);
	return messageHandler;
}
 
Example #14
Source File: KinesisMessageChannelBinder.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 5 votes vote down vote up
private AbstractAwsMessageHandler<?> createKinesisMessageHandler(ProducerDestination destination,
		FunctionExpression<Message<?>> partitionKeyExpression) {

	final KinesisMessageHandler messageHandler;
	messageHandler = new KinesisMessageHandler(this.amazonKinesis);
	messageHandler.setStream(destination.getName());
	messageHandler.setPartitionKeyExpression(partitionKeyExpression);
	return messageHandler;
}
 
Example #15
Source File: RocketMQMessageChannelBinder.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
protected MessageHandler createProducerMessageHandler(ProducerDestination destination,
		ExtendedProducerProperties<RocketMQProducerProperties> producerProperties,
		MessageChannel errorChannel) throws Exception {
	throw new UnsupportedOperationException(
			"The abstract binder should not call this method");
}
 
Example #16
Source File: RabbitMessageChannelBinder.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Override
protected MessageHandler createProducerMessageHandler(
		final ProducerDestination producerDestination,
		ExtendedProducerProperties<RabbitProducerProperties> producerProperties,
		MessageChannel errorChannel) {
	Assert.state(
			!HeaderMode.embeddedHeaders.equals(producerProperties.getHeaderMode()),
			"the RabbitMQ binder does not support embedded headers since RabbitMQ supports headers natively");
	String prefix = producerProperties.getExtension().getPrefix();
	String exchangeName = producerDestination.getName();
	String destination = StringUtils.isEmpty(prefix) ? exchangeName
			: exchangeName.substring(prefix.length());
	final AmqpOutboundEndpoint endpoint = new AmqpOutboundEndpoint(
			buildRabbitTemplate(producerProperties.getExtension(),
					errorChannel != null));
	endpoint.setExchangeName(producerDestination.getName());
	RabbitProducerProperties extendedProperties = producerProperties.getExtension();
	boolean expressionInterceptorNeeded = expressionInterceptorNeeded(
			extendedProperties);
	Expression routingKeyExpression = extendedProperties.getRoutingKeyExpression();
	if (!producerProperties.isPartitioned()) {
		if (routingKeyExpression == null) {
			endpoint.setRoutingKey(destination);
		}
		else {
			if (expressionInterceptorNeeded) {
				endpoint.setRoutingKeyExpressionString("headers['"
						+ RabbitExpressionEvaluatingInterceptor.ROUTING_KEY_HEADER
						+ "']");
			}
			else {
				endpoint.setRoutingKeyExpression(routingKeyExpression);
			}
		}
	}
	else {
		if (routingKeyExpression == null) {
			endpoint.setRoutingKeyExpression(
					buildPartitionRoutingExpression(destination, false));
		}
		else {
			if (expressionInterceptorNeeded) {
				endpoint.setRoutingKeyExpression(
						buildPartitionRoutingExpression("headers['"
								+ RabbitExpressionEvaluatingInterceptor.ROUTING_KEY_HEADER
								+ "']", true));
			}
			else {
				endpoint.setRoutingKeyExpression(buildPartitionRoutingExpression(
						routingKeyExpression.getExpressionString(), true));
			}
		}
	}
	if (extendedProperties.getDelayExpression() != null) {
		if (expressionInterceptorNeeded) {
			endpoint.setDelayExpressionString("headers['"
					+ RabbitExpressionEvaluatingInterceptor.DELAY_HEADER + "']");
		}
		else {
			endpoint.setDelayExpression(extendedProperties.getDelayExpression());
		}
	}
	DefaultAmqpHeaderMapper mapper = DefaultAmqpHeaderMapper.outboundMapper();
	List<String> headerPatterns = new ArrayList<>(extendedProperties.getHeaderPatterns().length + 3);
	headerPatterns.add("!" + BinderHeaders.PARTITION_HEADER);
	headerPatterns.add("!" + IntegrationMessageHeaderAccessor.SOURCE_DATA);
	headerPatterns.add("!" + IntegrationMessageHeaderAccessor.DELIVERY_ATTEMPT);
	headerPatterns.addAll(Arrays.asList(extendedProperties.getHeaderPatterns()));
	mapper.setRequestHeaderNames(
			headerPatterns.toArray(new String[headerPatterns.size()]));
	endpoint.setHeaderMapper(mapper);
	endpoint.setDefaultDeliveryMode(extendedProperties.getDeliveryMode());
	endpoint.setBeanFactory(this.getBeanFactory());
	if (errorChannel != null) {
		checkConnectionFactoryIsErrorCapable();
		endpoint.setReturnChannel(errorChannel);
		endpoint.setConfirmNackChannel(errorChannel);
		String ackChannelBeanName = StringUtils
				.hasText(extendedProperties.getConfirmAckChannel())
						? extendedProperties.getConfirmAckChannel()
						: IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME;
		if (!ackChannelBeanName.equals(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)
				&& !getApplicationContext().containsBean(ackChannelBeanName)) {
			GenericApplicationContext context = (GenericApplicationContext) getApplicationContext();
			context.registerBean(ackChannelBeanName, DirectChannel.class,
					() -> new DirectChannel());
		}
		endpoint.setConfirmAckChannelName(ackChannelBeanName);
		endpoint.setConfirmCorrelationExpressionString("#root");
		endpoint.setErrorMessageStrategy(new DefaultErrorMessageStrategy());
	}
	endpoint.setHeadersMappedLast(true);
	return endpoint;
}
 
Example #17
Source File: AbstractMessageChannelBinder.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
protected String getErrorBridgeName(ProducerDestination destination) {
	return errorsBaseName(destination) + ".bridge";
}
 
Example #18
Source File: AbstractMessageChannelBinder.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
protected String errorsBaseName(ProducerDestination destination) {
	return destination.getName() + ".errors";
}
 
Example #19
Source File: AbstractMessageChannelBinder.java    From spring-cloud-stream with Apache License 2.0 3 votes vote down vote up
/**
 * Create a {@link MessageHandler} with the ability to send data to the target
 * middleware. If the returned instance is also a {@link Lifecycle}, it will be
 * stopped automatically by the binder.
 * <p>
 * In order to be fully compliant, the {@link MessageHandler} of the binder must
 * observe the following headers:
 * <ul>
 * <li>{@link BinderHeaders#PARTITION_HEADER} - indicates the target partition where
 * the message must be sent</li>
 * </ul>
 * <p>
 * @param destination the name of the target destination.
 * @param producerProperties the producer properties.
 * @param channel the channel to bind.
 * @param errorChannel the error channel (if enabled, otherwise null). If not null,
 * the binder must wire this channel into the producer endpoint so that errors are
 * forwarded to it.
 * @return the message handler for sending data to the target middleware
 * @throws Exception when producer messsage handler failed to be created
 */
protected MessageHandler createProducerMessageHandler(ProducerDestination destination,
		P producerProperties, MessageChannel channel, MessageChannel errorChannel)
		throws Exception {
	return createProducerMessageHandler(destination, producerProperties,
			errorChannel);
}
 
Example #20
Source File: AbstractMessageChannelBinder.java    From spring-cloud-stream with Apache License 2.0 2 votes vote down vote up
/**
 * Create a {@link MessageHandler} with the ability to send data to the target
 * middleware. If the returned instance is also a {@link Lifecycle}, it will be
 * stopped automatically by the binder.
 * <p>
 * In order to be fully compliant, the {@link MessageHandler} of the binder must
 * observe the following headers:
 * <ul>
 * <li>{@link BinderHeaders#PARTITION_HEADER} - indicates the target partition where
 * the message must be sent</li>
 * </ul>
 * <p>
 * @param destination the name of the target destination
 * @param producerProperties the producer properties
 * @param errorChannel the error channel (if enabled, otherwise null). If not null,
 * the binder must wire this channel into the producer endpoint so that errors are
 * forwarded to it.
 * @return the message handler for sending data to the target middleware
 * @throws Exception upon failure to create the producer message handler
 */
protected abstract MessageHandler createProducerMessageHandler(
		ProducerDestination destination, P producerProperties,
		MessageChannel errorChannel) throws Exception;
 
Example #21
Source File: AbstractMessageChannelBinder.java    From spring-cloud-stream with Apache License 2.0 2 votes vote down vote up
/**
 * Invoked after the unbinding of a producer. Subclasses may override this to provide
 * their own logic for dealing with unbinding.
 * @param destination the bound destination
 * @param producerProperties the producer properties
 */
protected void afterUnbindProducer(ProducerDestination destination,
		P producerProperties) {
}