├── pulsar-spring-cloud-stream-binder ├── src │ ├── main │ │ ├── resources │ │ │ └── META-INF │ │ │ │ ├── spring.binders │ │ │ │ └── spring.factories │ │ └── java │ │ │ └── com │ │ │ └── datastax │ │ │ └── oss │ │ │ └── pulsar │ │ │ └── springcloudstream │ │ │ ├── config │ │ │ ├── PulsarClientAutoConfiguration.java │ │ │ ├── PulsarClientConfigurationProperties.java │ │ │ └── PulsarBinderConfiguration.java │ │ │ ├── properties │ │ │ ├── SchemaSpec.java │ │ │ ├── PulsarBinderConfigurationProperties.java │ │ │ ├── PulsarProducerProperties.java │ │ │ ├── PulsarBindingProperties.java │ │ │ ├── PulsarConsumerProperties.java │ │ │ └── PulsarExtendedBindingProperties.java │ │ │ ├── provisioning │ │ │ ├── PulsarConsumerDestination.java │ │ │ ├── PulsarProducerDestination.java │ │ │ └── PulsarTopicProvisioner.java │ │ │ ├── PulsarConsumerEndpoint.java │ │ │ ├── PulsarProducerMessageHandler.java │ │ │ └── PulsarMessageChannelBinder.java │ └── test │ │ ├── resources │ │ └── logback-test.xml │ │ └── java │ │ └── com │ │ └── datastax │ │ └── oss │ │ └── pulsar │ │ └── springcloudstream │ │ ├── provisioning │ │ └── PulsarTopicProvisionerTests.java │ │ ├── PulsarContainerTest.java │ │ ├── PulsarTestBinder.java │ │ ├── PulsarBinderTests.java │ │ └── PulsarBinderFunctionalTests.java └── pom.xml ├── README.md ├── .gitignore ├── pom.xml └── LICENSE /pulsar-spring-cloud-stream-binder/src/main/resources/META-INF/spring.binders: -------------------------------------------------------------------------------- 1 | pulsar: com.datastax.oss.pulsar.springcloudstream.config.PulsarBinderConfiguration 2 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.datastax.oss.pulsar.springcloudstream.config.PulsarClientAutoConfiguration -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pulsar-spring-cloud-stream-binder 2 | 3 | Apache Pulsar [binder](https://github.com/spring-cloud/spring-cloud-stream/blob/main/docs/src/main/asciidoc/spring-cloud-stream.adoc#binders) for Spring Cloud Stream 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | apps/ 2 | /application.yml 3 | /application.properties 4 | asciidoctor.css 5 | *~ 6 | .#* 7 | *# 8 | target/ 9 | build/ 10 | bin/ 11 | _site/ 12 | .classpath 13 | .project 14 | .settings 15 | .springBeans 16 | .DS_Store 17 | *.sw* 18 | *.iml 19 | *.ipr 20 | *.iws 21 | .idea/ 22 | .factorypath 23 | spring-xd-samples/*/xd 24 | dump.rdb 25 | coverage-error.log 26 | .apt_generated 27 | aws.credentials.properties 28 | nb-configuration.xml 29 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/test/java/com/datastax/oss/pulsar/springcloudstream/provisioning/PulsarTopicProvisionerTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream.provisioning; 18 | 19 | /** 20 | * Tests for the {@link PulsarTopicProvisioner}. 21 | * 22 | * @author Lari Hotari 23 | */ 24 | class PulsarTopicProvisionerTests { 25 | 26 | } 27 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/config/PulsarClientAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.datastax.oss.pulsar.springcloudstream.config; 2 | 3 | import org.apache.pulsar.client.api.PulsarClient; 4 | import org.apache.pulsar.client.api.PulsarClientException; 5 | import org.apache.pulsar.client.impl.ClientBuilderImpl; 6 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 7 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.context.annotation.Lazy; 11 | 12 | @Configuration(proxyBeanMethods = false) 13 | @EnableConfigurationProperties(PulsarClientConfigurationProperties.class) 14 | public class PulsarClientAutoConfiguration { 15 | @Bean 16 | @Lazy 17 | @ConditionalOnMissingBean 18 | PulsarClient pulsarClient( 19 | PulsarClientConfigurationProperties pulsarClientConfigurationProperties) 20 | throws PulsarClientException { 21 | return new ClientBuilderImpl(pulsarClientConfigurationProperties).build(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/properties/SchemaSpec.java: -------------------------------------------------------------------------------- 1 | package com.datastax.oss.pulsar.springcloudstream.properties; 2 | 3 | import org.apache.pulsar.client.api.Schema; 4 | import org.apache.pulsar.common.schema.SchemaType; 5 | 6 | public class SchemaSpec { 7 | private SchemaType type = SchemaType.BYTES; 8 | private Class valueClass = byte[].class; 9 | 10 | public SchemaType getType() { 11 | return type; 12 | } 13 | 14 | public void setType(SchemaType type) { 15 | this.type = type; 16 | switch (type) { 17 | case STRING: 18 | valueClass = String.class; 19 | break; 20 | case BYTES: 21 | valueClass = byte[].class; 22 | break; 23 | } 24 | } 25 | 26 | public Class getValueClass() { 27 | return valueClass; 28 | } 29 | 30 | public void setValueClass(Class valueClass) { 31 | this.valueClass = valueClass; 32 | } 33 | 34 | public Schema asPulsarSchema() { 35 | switch (type) { 36 | case STRING: 37 | return Schema.STRING; 38 | case AVRO: 39 | return Schema.AVRO(valueClass); 40 | case JSON: 41 | return Schema.JSON(valueClass); 42 | } 43 | 44 | return Schema.BYTES; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/provisioning/PulsarConsumerDestination.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream.provisioning; 18 | 19 | import org.springframework.cloud.stream.provisioning.ConsumerDestination; 20 | 21 | /** 22 | * The Pulsar-specific {@link ConsumerDestination} implementation. 23 | * 24 | * @author Lari Hotari 25 | * 26 | */ 27 | public final class PulsarConsumerDestination implements ConsumerDestination { 28 | 29 | private final String topicName; 30 | 31 | public PulsarConsumerDestination(String topicName) { 32 | this.topicName = topicName; 33 | } 34 | 35 | @Override 36 | public String getName() { 37 | return this.topicName; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/properties/PulsarBinderConfigurationProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream.properties; 18 | 19 | import org.springframework.boot.context.properties.ConfigurationProperties; 20 | 21 | /** 22 | * The Pulsar Binder specific configuration properties. 23 | * 24 | * @author Lari Hotari 25 | */ 26 | @ConfigurationProperties(prefix = "pulsar.spring.cloud.stream.binder") 27 | public class PulsarBinderConfigurationProperties { 28 | private String[] headers = new String[] { }; 29 | 30 | public String[] getHeaders() { 31 | return this.headers; 32 | } 33 | 34 | public void setHeaders(String... headers) { 35 | this.headers = headers; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/provisioning/PulsarProducerDestination.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream.provisioning; 18 | 19 | import org.springframework.cloud.stream.provisioning.ProducerDestination; 20 | 21 | /** 22 | * The Pulsar-specific {@link ProducerDestination} implementation. 23 | * 24 | * @author Lari Hotari 25 | * 26 | */ 27 | public final class PulsarProducerDestination implements ProducerDestination { 28 | 29 | private final String topicName; 30 | 31 | PulsarProducerDestination(String topicName) { 32 | this.topicName = topicName; 33 | } 34 | 35 | @Override 36 | public String getName() { 37 | return this.topicName; 38 | } 39 | 40 | @Override 41 | public String getNameForPartition(int partition) { 42 | return this.topicName + "-partition-" + partition; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/properties/PulsarProducerProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream.properties; 18 | 19 | import org.apache.pulsar.client.impl.conf.ProducerConfigurationData; 20 | 21 | /** 22 | * The Pulsar-specific producer binding configuration properties. 23 | * 24 | * @author Lari Hotari 25 | * 26 | */ 27 | public class PulsarProducerProperties extends ProducerConfigurationData { 28 | private boolean useSendAsync; 29 | private SchemaSpec schema = new SchemaSpec(); 30 | public PulsarProducerProperties() { 31 | setBlockIfQueueFull(true); 32 | } 33 | 34 | public boolean isUseSendAsync() { 35 | return useSendAsync; 36 | } 37 | 38 | public void setUseSendAsync(boolean useSendAsync) { 39 | this.useSendAsync = useSendAsync; 40 | } 41 | 42 | public SchemaSpec getSchema() { 43 | return schema; 44 | } 45 | 46 | public void setSchema(SchemaSpec schema) { 47 | this.schema = schema; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/properties/PulsarBindingProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream.properties; 18 | 19 | import org.springframework.cloud.stream.binder.BinderSpecificPropertiesProvider; 20 | 21 | /** 22 | * The Pulsar-specific binding configuration properties. 23 | * 24 | * @author Lari Hotari 25 | */ 26 | public class PulsarBindingProperties implements BinderSpecificPropertiesProvider { 27 | 28 | private PulsarConsumerProperties consumer = new PulsarConsumerProperties(); 29 | 30 | private PulsarProducerProperties producer = new PulsarProducerProperties(); 31 | 32 | public PulsarConsumerProperties getConsumer() { 33 | return this.consumer; 34 | } 35 | 36 | public void setConsumer(PulsarConsumerProperties consumer) { 37 | this.consumer = consumer; 38 | } 39 | 40 | public PulsarProducerProperties getProducer() { 41 | return this.producer; 42 | } 43 | 44 | public void setProducer(PulsarProducerProperties producer) { 45 | this.producer = producer; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/properties/PulsarConsumerProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream.properties; 18 | 19 | import org.apache.pulsar.client.api.SubscriptionInitialPosition; 20 | import org.apache.pulsar.client.api.SubscriptionType; 21 | import org.apache.pulsar.client.impl.conf.ConsumerConfigurationData; 22 | 23 | /** 24 | * The Pulsar-specific consumer binding configuration properties. 25 | * 26 | * @author Lari Hotari 27 | * 28 | */ 29 | public class PulsarConsumerProperties extends ConsumerConfigurationData { 30 | private SchemaSpec schema = new SchemaSpec(); 31 | private String contentType; 32 | 33 | public PulsarConsumerProperties() { 34 | setStartPaused(true); 35 | setSubscriptionInitialPosition(SubscriptionInitialPosition.Earliest); 36 | setSubscriptionType(SubscriptionType.Shared); 37 | } 38 | 39 | public SchemaSpec getSchema() { 40 | return schema; 41 | } 42 | 43 | public void setSchema(SchemaSpec schema) { 44 | this.schema = schema; 45 | } 46 | 47 | public String getContentType() { 48 | return contentType; 49 | } 50 | 51 | public void setContentType(String contentType) { 52 | this.contentType = contentType; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | com.datastax.oss 8 | pulsar-spring-cloud-stream-binder-parent 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | pulsar-spring-cloud-stream-binder 13 | 14 | jar 15 | pulsar-spring-cloud-stream-binder 16 | Apache Pulsar Binder Implementation for Spring Cloud Stream 17 | 18 | 19 | 20 | org.apache.pulsar 21 | pulsar-client 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-stream 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-stream-binder-test 30 | test 31 | 32 | 33 | org.springframework.cloud 34 | spring-cloud-stream-test-support-internal 35 | test 36 | 37 | 38 | javax.validation 39 | validation-api 40 | 2.0.1.Final 41 | test 42 | 43 | 44 | org.testcontainers 45 | junit-jupiter 46 | test 47 | 48 | 49 | org.testcontainers 50 | pulsar 51 | test 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/test/java/com/datastax/oss/pulsar/springcloudstream/PulsarContainerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream; 18 | 19 | import org.apache.pulsar.client.api.PulsarClient; 20 | import org.apache.pulsar.client.api.PulsarClientException; 21 | import org.springframework.test.context.DynamicPropertyRegistry; 22 | import org.testcontainers.containers.PulsarContainer; 23 | import org.testcontainers.junit.jupiter.Container; 24 | import org.testcontainers.junit.jupiter.Testcontainers; 25 | import org.testcontainers.utility.DockerImageName; 26 | 27 | /** 28 | * @author Lari Hotari 29 | */ 30 | @Testcontainers(disabledWithoutDocker = true) 31 | public interface PulsarContainerTest { 32 | 33 | @Container 34 | PulsarContainer pulsarContainer = new PulsarContainer( 35 | DockerImageName.parse("apachepulsar/pulsar:2.10.0")); 36 | 37 | static PulsarClient pulsarClient() throws PulsarClientException { 38 | PulsarClient pulsarClient = PulsarClient.builder() 39 | .serviceUrl(pulsarContainer.getPulsarBrokerUrl()).build(); 40 | return pulsarClient; 41 | } 42 | 43 | public static void register(DynamicPropertyRegistry registry) { 44 | registry.add("pulsar.client.serviceUrl", pulsarContainer::getPulsarBrokerUrl); 45 | // TODO: this property is currently unused 46 | registry.add("pulsar.admin.serviceHttpUrl", pulsarContainer::getHttpServiceUrl); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/config/PulsarClientConfigurationProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream.config; 18 | 19 | import org.apache.pulsar.client.api.AuthenticationFactory; 20 | import org.apache.pulsar.client.impl.conf.ClientConfigurationData; 21 | import org.springframework.beans.factory.InitializingBean; 22 | import org.springframework.boot.context.properties.ConfigurationProperties; 23 | 24 | @ConfigurationProperties("pulsar.client") 25 | public class PulsarClientConfigurationProperties extends ClientConfigurationData implements InitializingBean { 26 | 27 | public PulsarClientConfigurationProperties() { 28 | setServiceUrl("pulsar://localhost:6650/"); 29 | } 30 | 31 | void setIoThreads(int ioThreads) { 32 | setNumIoThreads(ioThreads); 33 | } 34 | 35 | void setListenerThreads(int listenerThreads) { 36 | setNumListenerThreads(listenerThreads); 37 | } 38 | 39 | void setMaxLookupRequests(int maxLookupRequests) { 40 | setMaxLookupRequest(maxLookupRequests); 41 | } 42 | 43 | void setMaxConcurrentLookupRequests(int maxConcurrentLookupRequests) { 44 | setConcurrentLookupRequest(maxConcurrentLookupRequests); 45 | } 46 | 47 | @Override 48 | public void afterPropertiesSet() throws Exception { 49 | if (getAuthPluginClassName() != null) { 50 | if (getAuthParams() != null) { 51 | setAuthentication(AuthenticationFactory.create(getAuthPluginClassName(), getAuthParams())); 52 | } else if (getAuthParamMap() != null) { 53 | setAuthentication(AuthenticationFactory.create(getAuthPluginClassName(), getAuthParamMap())); 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/properties/PulsarExtendedBindingProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream.properties; 18 | 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | 22 | import org.springframework.boot.context.properties.ConfigurationProperties; 23 | import org.springframework.cloud.stream.binder.BinderSpecificPropertiesProvider; 24 | import org.springframework.cloud.stream.binder.ExtendedBindingProperties; 25 | 26 | /** 27 | * The extended Pulsar-specific binding configuration properties. 28 | * 29 | * @author Lari Hotari 30 | * 31 | */ 32 | @ConfigurationProperties("pulsar.spring.cloud.stream") 33 | public class PulsarExtendedBindingProperties implements 34 | ExtendedBindingProperties { 35 | 36 | private static final String DEFAULTS_PREFIX = "pulsar.spring.cloud.stream.default"; 37 | 38 | private Map bindings = new HashMap<>(); 39 | 40 | public Map getBindings() { 41 | return this.bindings; 42 | } 43 | 44 | public void setBindings(Map bindings) { 45 | this.bindings = bindings; 46 | } 47 | 48 | @Override 49 | public PulsarConsumerProperties getExtendedConsumerProperties(String channelName) { 50 | if (this.bindings.containsKey(channelName) 51 | && this.bindings.get(channelName).getConsumer() != null) { 52 | return this.bindings.get(channelName).getConsumer(); 53 | } 54 | else { 55 | return new PulsarConsumerProperties(); 56 | } 57 | } 58 | 59 | @Override 60 | public PulsarProducerProperties getExtendedProducerProperties(String channelName) { 61 | if (this.bindings.containsKey(channelName) 62 | && this.bindings.get(channelName).getProducer() != null) { 63 | return this.bindings.get(channelName).getProducer(); 64 | } 65 | else { 66 | return new PulsarProducerProperties(); 67 | } 68 | } 69 | 70 | @Override 71 | public String getDefaultsPrefix() { 72 | return DEFAULTS_PREFIX; 73 | } 74 | 75 | @Override 76 | public Class getExtendedPropertiesEntryClass() { 77 | return PulsarBindingProperties.class; 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/provisioning/PulsarTopicProvisioner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream.provisioning; 18 | 19 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarBinderConfigurationProperties; 20 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarConsumerProperties; 21 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarProducerProperties; 22 | import org.apache.commons.logging.Log; 23 | import org.apache.commons.logging.LogFactory; 24 | import org.springframework.cloud.stream.binder.ExtendedConsumerProperties; 25 | import org.springframework.cloud.stream.binder.ExtendedProducerProperties; 26 | import org.springframework.cloud.stream.provisioning.ConsumerDestination; 27 | import org.springframework.cloud.stream.provisioning.ProducerDestination; 28 | import org.springframework.cloud.stream.provisioning.ProvisioningException; 29 | import org.springframework.cloud.stream.provisioning.ProvisioningProvider; 30 | 31 | /** 32 | * The {@link ProvisioningProvider} implementation for Apache Pulsar. 33 | * 34 | * @author Lari Hotari 35 | */ 36 | public class PulsarTopicProvisioner implements 37 | ProvisioningProvider, 38 | ExtendedProducerProperties> { 39 | 40 | private static final Log logger = LogFactory.getLog(PulsarTopicProvisioner.class); 41 | 42 | private final PulsarBinderConfigurationProperties configurationProperties; 43 | 44 | public PulsarTopicProvisioner( 45 | PulsarBinderConfigurationProperties pulsarBinderConfigurationProperties) { 46 | this.configurationProperties = pulsarBinderConfigurationProperties; 47 | } 48 | 49 | @Override 50 | public ProducerDestination provisionProducerDestination(String name, 51 | ExtendedProducerProperties properties) 52 | throws ProvisioningException { 53 | 54 | if (logger.isInfoEnabled()) { 55 | logger.info("Using Pulsar topic for outbound: " + name); 56 | } 57 | 58 | return new PulsarProducerDestination(name); 59 | } 60 | 61 | @Override 62 | public ConsumerDestination provisionConsumerDestination(String name, String group, 63 | ExtendedConsumerProperties properties) 64 | throws ProvisioningException { 65 | 66 | if (logger.isInfoEnabled()) { 67 | logger.info("Using Pulsar topic for inbound: " + name); 68 | } 69 | 70 | return new PulsarConsumerDestination(name); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/config/PulsarBinderConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream.config; 18 | 19 | import org.apache.pulsar.client.api.PulsarClient; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.beans.factory.annotation.Qualifier; 22 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 23 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 24 | import org.springframework.cloud.stream.binder.Binder; 25 | import org.springframework.context.annotation.Bean; 26 | import org.springframework.context.annotation.Configuration; 27 | import org.springframework.context.annotation.Import; 28 | 29 | import com.datastax.oss.pulsar.springcloudstream.PulsarMessageChannelBinder; 30 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarBinderConfigurationProperties; 31 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarExtendedBindingProperties; 32 | import com.datastax.oss.pulsar.springcloudstream.provisioning.PulsarTopicProvisioner; 33 | import org.springframework.integration.context.IntegrationContextUtils; 34 | import org.springframework.messaging.converter.MessageConverter; 35 | 36 | /** 37 | * The auto-configuration for Apache Pulsar components and Spring Cloud Stream Pulsar 38 | * Binder. 39 | * 40 | * @author Lari Hotari 41 | */ 42 | @Configuration(proxyBeanMethods = false) 43 | @ConditionalOnMissingBean(Binder.class) 44 | @EnableConfigurationProperties({ PulsarBinderConfigurationProperties.class, 45 | PulsarExtendedBindingProperties.class }) 46 | @Import(PulsarClientAutoConfiguration.class) 47 | public class PulsarBinderConfiguration { 48 | 49 | private final PulsarBinderConfigurationProperties configurationProperties; 50 | private final MessageConverter messageConverter; 51 | 52 | public PulsarBinderConfiguration( 53 | PulsarBinderConfigurationProperties configurationProperties, 54 | @Autowired(required = false) @Qualifier(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME) MessageConverter messageConverter) { 55 | this.configurationProperties = configurationProperties; 56 | this.messageConverter = messageConverter; 57 | } 58 | 59 | @Bean 60 | public PulsarTopicProvisioner pulsarTopicProvisioner() { 61 | return new PulsarTopicProvisioner(configurationProperties); 62 | } 63 | 64 | @Bean 65 | public PulsarMessageChannelBinder pulsarMessageChannelBinder( 66 | PulsarClient pulsarClient, PulsarTopicProvisioner provisioningProvider, 67 | PulsarExtendedBindingProperties pulsarExtendedBindingProperties) { 68 | 69 | PulsarMessageChannelBinder pulsarMessageChannelBinder = new PulsarMessageChannelBinder( 70 | this.configurationProperties, provisioningProvider, pulsarClient, 71 | pulsarExtendedBindingProperties, messageConverter); 72 | return pulsarMessageChannelBinder; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/test/java/com/datastax/oss/pulsar/springcloudstream/PulsarTestBinder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream; 18 | 19 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarBinderConfigurationProperties; 20 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarConsumerProperties; 21 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarExtendedBindingProperties; 22 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarProducerProperties; 23 | import com.datastax.oss.pulsar.springcloudstream.provisioning.PulsarTopicProvisioner; 24 | import org.apache.pulsar.client.api.PulsarClient; 25 | import org.springframework.cloud.stream.binder.AbstractTestBinder; 26 | import org.springframework.cloud.stream.binder.ExtendedConsumerProperties; 27 | import org.springframework.cloud.stream.binder.ExtendedProducerProperties; 28 | import org.springframework.cloud.stream.binder.PartitionTestSupport; 29 | import org.springframework.cloud.stream.provisioning.ConsumerDestination; 30 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 31 | import org.springframework.context.annotation.Bean; 32 | import org.springframework.context.annotation.Configuration; 33 | import org.springframework.context.support.GenericApplicationContext; 34 | import org.springframework.integration.config.EnableIntegration; 35 | import org.springframework.messaging.converter.MessageConverter; 36 | import org.springframework.messaging.converter.SimpleMessageConverter; 37 | 38 | /** 39 | * An {@link AbstractTestBinder} implementation for the 40 | * {@link PulsarMessageChannelBinder}. 41 | * 42 | * @author Lari Hotari 43 | */ 44 | public class PulsarTestBinder extends 45 | AbstractTestBinder, ExtendedProducerProperties> { 46 | 47 | private final GenericApplicationContext applicationContext; 48 | private final PulsarClient pulsarClient; 49 | 50 | public PulsarTestBinder(PulsarClient pulsarClient, 51 | PulsarBinderConfigurationProperties pulsarBinderConfigurationProperties) { 52 | this.pulsarClient = pulsarClient; 53 | 54 | this.applicationContext = new AnnotationConfigApplicationContext(Config.class); 55 | 56 | PulsarTopicProvisioner provisioningProvider = new PulsarTopicProvisioner( 57 | pulsarBinderConfigurationProperties); 58 | 59 | PulsarMessageChannelBinder binder = new TestPulsarMessageChannelBinder( 60 | pulsarClient, pulsarBinderConfigurationProperties, provisioningProvider, 61 | new SimpleMessageConverter()); 62 | 63 | binder.setApplicationContext(this.applicationContext); 64 | 65 | setBinder(binder); 66 | } 67 | 68 | public GenericApplicationContext getApplicationContext() { 69 | return this.applicationContext; 70 | } 71 | 72 | @Override 73 | public void cleanup() { 74 | 75 | } 76 | 77 | /** 78 | * Test configuration. 79 | */ 80 | @Configuration 81 | @EnableIntegration 82 | static class Config { 83 | 84 | @Bean 85 | public PartitionTestSupport partitionSupport() { 86 | return new PartitionTestSupport(); 87 | } 88 | 89 | } 90 | 91 | private static class TestPulsarMessageChannelBinder 92 | extends PulsarMessageChannelBinder { 93 | 94 | TestPulsarMessageChannelBinder(PulsarClient pulsarClient, 95 | PulsarBinderConfigurationProperties pulsarBinderConfigurationProperties, 96 | PulsarTopicProvisioner provisioningProvider, 97 | MessageConverter messageConverter) { 98 | 99 | super(pulsarBinderConfigurationProperties, provisioningProvider, pulsarClient, 100 | new PulsarExtendedBindingProperties(), messageConverter); 101 | } 102 | 103 | /* 104 | * Some tests use multiple instance indexes for the same topic; we need to make 105 | * the error infrastructure beans unique. 106 | */ 107 | @Override 108 | protected String errorsBaseName(ConsumerDestination destination, String group, 109 | ExtendedConsumerProperties consumerProperties) { 110 | return super.errorsBaseName(destination, group, consumerProperties) + "-" 111 | + consumerProperties.getInstanceIndex(); 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/test/java/com/datastax/oss/pulsar/springcloudstream/PulsarBinderTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream; 18 | 19 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarBinderConfigurationProperties; 20 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarConsumerProperties; 21 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarProducerProperties; 22 | import org.apache.pulsar.client.api.PulsarClient; 23 | import org.apache.pulsar.client.api.PulsarClientException; 24 | import org.junit.jupiter.api.AfterAll; 25 | import org.junit.jupiter.api.BeforeAll; 26 | import org.junit.jupiter.api.Disabled; 27 | import org.junit.jupiter.api.TestInfo; 28 | import org.springframework.cloud.stream.binder.ExtendedConsumerProperties; 29 | import org.springframework.cloud.stream.binder.ExtendedProducerProperties; 30 | import org.springframework.cloud.stream.binder.PartitionCapableBinderTests; 31 | import org.springframework.cloud.stream.binder.Spy; 32 | import org.springframework.expression.common.LiteralExpression; 33 | 34 | /** 35 | * The tests for Pulsar Binder. 36 | * 37 | * @author Lari Hotari 38 | */ 39 | public class PulsarBinderTests extends 40 | PartitionCapableBinderTests, ExtendedProducerProperties> 41 | implements PulsarContainerTest { 42 | 43 | private static final String CLASS_UNDER_TEST_NAME = PulsarBinderTests.class 44 | .getSimpleName(); 45 | 46 | private static PulsarClient PULSAR_CLIENT; 47 | 48 | public PulsarBinderTests() { 49 | this.timeoutMultiplier = 10D; 50 | } 51 | 52 | @BeforeAll 53 | public static void setup() throws PulsarClientException { 54 | PULSAR_CLIENT = PulsarContainerTest.pulsarClient(); 55 | } 56 | 57 | @AfterAll 58 | public static void closePulsarClient() throws PulsarClientException { 59 | PULSAR_CLIENT.close(); 60 | } 61 | 62 | @Override 63 | protected boolean usesExplicitRouting() { 64 | return false; 65 | } 66 | 67 | @Override 68 | protected String getClassUnderTestName() { 69 | return CLASS_UNDER_TEST_NAME; 70 | } 71 | 72 | @Override 73 | protected PulsarTestBinder getBinder() { 74 | return getBinder(new PulsarBinderConfigurationProperties()); 75 | } 76 | 77 | private PulsarTestBinder getBinder( 78 | PulsarBinderConfigurationProperties pulsarBinderConfigurationProperties) { 79 | if (this.testBinder == null) { 80 | this.testBinder = new PulsarTestBinder(PULSAR_CLIENT, 81 | pulsarBinderConfigurationProperties); 82 | this.timeoutMultiplier = 20; 83 | } 84 | return this.testBinder; 85 | } 86 | 87 | @Override 88 | protected ExtendedConsumerProperties createConsumerProperties() { 89 | ExtendedConsumerProperties pulsarConsumerProperties = new ExtendedConsumerProperties<>( 90 | new PulsarConsumerProperties()); 91 | // set the default values that would normally be propagated by Spring Cloud Stream 92 | pulsarConsumerProperties.setInstanceCount(1); 93 | pulsarConsumerProperties.setInstanceIndex(0); 94 | return pulsarConsumerProperties; 95 | } 96 | 97 | private ExtendedProducerProperties createProducerProperties() { 98 | return this.createProducerProperties(null); 99 | } 100 | 101 | @Override 102 | protected ExtendedProducerProperties createProducerProperties( 103 | TestInfo testInto) { 104 | ExtendedProducerProperties producerProperties = new ExtendedProducerProperties<>( 105 | new PulsarProducerProperties()); 106 | producerProperties.setPartitionKeyExpression(new LiteralExpression("1")); 107 | return producerProperties; 108 | } 109 | 110 | @Override 111 | public Spy spyOn(String name) { 112 | throw new UnsupportedOperationException("'spyOn' is not used by Pulsar tests"); 113 | } 114 | 115 | @Override 116 | @Disabled 117 | public void testAnonymousGroup(TestInfo testInfo) throws Exception { 118 | // this doesn't make sense in Pulsar because of the receive queue that consumes 119 | // messages 120 | } 121 | 122 | @Override 123 | @Disabled 124 | public void testClean(TestInfo testInfo) { 125 | } 126 | 127 | @Override 128 | @Disabled 129 | public void testPartitionedModuleSpEL(TestInfo testInfo) { 130 | 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/PulsarConsumerEndpoint.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream; 18 | 19 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarConsumerProperties; 20 | import com.datastax.oss.pulsar.springcloudstream.properties.SchemaSpec; 21 | import java.lang.reflect.Field; 22 | import java.util.HashMap; 23 | import org.apache.pulsar.client.api.Consumer; 24 | import org.apache.pulsar.client.api.ConsumerBuilder; 25 | import org.apache.pulsar.client.api.PulsarClient; 26 | import org.apache.pulsar.client.api.PulsarClientException; 27 | import org.apache.pulsar.client.api.Schema; 28 | import org.apache.pulsar.client.api.SubscriptionInitialPosition; 29 | import org.apache.pulsar.client.api.SubscriptionType; 30 | import org.apache.pulsar.client.impl.ConsumerBuilderImpl; 31 | import org.apache.pulsar.client.impl.conf.ConsumerConfigurationData; 32 | import org.springframework.cloud.stream.binder.ExtendedConsumerProperties; 33 | import org.springframework.cloud.stream.provisioning.ConsumerDestination; 34 | import org.springframework.integration.core.MessageProducer; 35 | import org.springframework.integration.core.Pausable; 36 | import org.springframework.messaging.MessageChannel; 37 | import org.springframework.messaging.support.GenericMessage; 38 | import org.springframework.util.ReflectionUtils; 39 | 40 | class PulsarConsumerEndpoint implements MessageProducer, Pausable { 41 | private final Consumer pulsarConsumer; 42 | private MessageChannel outputChannel; 43 | 44 | private static final Field CONF_FIELD = ReflectionUtils.findField( 45 | ConsumerBuilderImpl.class, "conf", ConsumerConfigurationData.class); 46 | static { 47 | ReflectionUtils.makeAccessible(CONF_FIELD); 48 | } 49 | 50 | private volatile boolean running; 51 | 52 | private final SchemaSpec schemaSpec; 53 | 54 | private final String contentType; 55 | 56 | PulsarConsumerEndpoint(PulsarClient pulsarClient, ConsumerDestination destination, 57 | String group, 58 | ExtendedConsumerProperties properties) { 59 | try { 60 | schemaSpec = properties.getExtension().getSchema(); 61 | contentType = properties.getExtension().getContentType(); 62 | ConsumerBuilder consumerBuilder = pulsarClient 63 | .newConsumer((Schema) schemaSpec.asPulsarSchema()); 64 | ConsumerConfigurationData consumerProperties = properties.getExtension() 65 | .clone(); 66 | // Use reflection since the Pulsar API doesn't have a public way to apply the 67 | // configuration object 68 | ReflectionUtils.setField(CONF_FIELD, consumerBuilder, consumerProperties); 69 | consumerBuilder.topic(destination.getName()) 70 | .messageListener(this::consumeMessage); 71 | if (consumerProperties.getSubscriptionName() == null) { 72 | if (group == null || group.isBlank()) { 73 | consumerBuilder.subscriptionName("anonymous"); 74 | } 75 | else { 76 | consumerBuilder.subscriptionName(group); 77 | } 78 | } 79 | pulsarConsumer = consumerBuilder.subscribe(); 80 | } 81 | catch (PulsarClientException e) { 82 | throw new RuntimeException(e); 83 | } 84 | } 85 | 86 | private void consumeMessage(Consumer consumer, 87 | org.apache.pulsar.client.api.Message message) { 88 | HashMap headers = new HashMap<>(message.getProperties()); 89 | if (contentType != null) { 90 | headers.put("contentType", contentType); 91 | } 92 | GenericMessage msg = new GenericMessage<>(message.getValue(), headers); 93 | outputChannel.send(msg); 94 | try { 95 | consumer.acknowledge(message); 96 | } 97 | catch (PulsarClientException e) { 98 | throw new RuntimeException(e); 99 | } 100 | } 101 | 102 | @Override 103 | public void setOutputChannel(MessageChannel outputChannel) { 104 | this.outputChannel = outputChannel; 105 | } 106 | 107 | @Override 108 | public MessageChannel getOutputChannel() { 109 | return outputChannel; 110 | } 111 | 112 | @Override 113 | public void pause() { 114 | pulsarConsumer.pause(); 115 | } 116 | 117 | @Override 118 | public void resume() { 119 | pulsarConsumer.resume(); 120 | } 121 | 122 | @Override 123 | public void start() { 124 | running = true; 125 | pulsarConsumer.resume(); 126 | } 127 | 128 | @Override 129 | public void stop() { 130 | try { 131 | running = false; 132 | pulsarConsumer.close(); 133 | } 134 | catch (PulsarClientException e) { 135 | throw new RuntimeException(e); 136 | } 137 | } 138 | 139 | @Override 140 | public boolean isRunning() { 141 | return running; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/test/java/com/datastax/oss/pulsar/springcloudstream/PulsarBinderFunctionalTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream; 18 | 19 | import static org.assertj.core.api.Assertions.assertThat; 20 | 21 | import java.util.List; 22 | import java.util.concurrent.ArrayBlockingQueue; 23 | import java.util.concurrent.BlockingQueue; 24 | import java.util.concurrent.CountDownLatch; 25 | import java.util.concurrent.TimeUnit; 26 | import java.util.function.Consumer; 27 | 28 | import org.apache.pulsar.client.api.PulsarClient; 29 | import org.apache.pulsar.client.api.PulsarClientException; 30 | import org.junit.jupiter.api.Test; 31 | import org.springframework.beans.factory.annotation.Autowired; 32 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 33 | import org.springframework.boot.test.context.SpringBootTest; 34 | import org.springframework.cloud.stream.function.StreamBridge; 35 | import org.springframework.context.annotation.Bean; 36 | import org.springframework.context.annotation.Configuration; 37 | import org.springframework.messaging.Message; 38 | import org.springframework.messaging.support.GenericMessage; 39 | import org.springframework.messaging.support.MessageBuilder; 40 | import org.springframework.test.annotation.DirtiesContext; 41 | import org.springframework.test.context.DynamicPropertyRegistry; 42 | import org.springframework.test.context.DynamicPropertySource; 43 | import org.springframework.util.MimeType; 44 | 45 | /** 46 | * @author Lari Hotari 47 | */ 48 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, properties = { 49 | "spring.cloud.stream.bindings.myEventConsumer-in-0.destination=" 50 | + PulsarBinderFunctionalTests.PULSAR_TOPIC, 51 | // example of setting Pulsar consumer settings for consumer 52 | "pulsar.spring.cloud.stream.bindings.myEventConsumer-in-0.consumer.subscriptionType=Failover", 53 | "pulsar.spring.cloud.stream.bindings.myEventConsumer-in-0.consumer.subscriptionName=MySubscription", 54 | "pulsar.spring.cloud.stream.bindings.myEventConsumer-in-0.consumer.schema.type=BYTES", 55 | "spring.cloud.stream.bindings.myEventProducer-out-0.destination=" 56 | + PulsarBinderFunctionalTests.PULSAR_TOPIC, 57 | // example of enabling error channel for a producer 58 | "spring.cloud.stream.bindings.myEventProducer-out-0.producer.errorChannelEnabled=true", 59 | // example of enabling useSendAsync for a producer 60 | "pulsar.spring.cloud.stream.bindings.myEventProducer-out-0.producer.useSendAsync=true", 61 | "pulsar.spring.cloud.stream.bindings.myEventProducer-out-0.producer.schema.type=STRING", 62 | "pulsar.spring.cloud.stream.binder.headers = event.eventType" }) 63 | @DirtiesContext 64 | public class PulsarBinderFunctionalTests implements PulsarContainerTest { 65 | @DynamicPropertySource 66 | static void registerPulsarProperties(DynamicPropertyRegistry registry) { 67 | PulsarContainerTest.register(registry); 68 | } 69 | 70 | static final String PULSAR_TOPIC = "test_topic"; 71 | public static final int NUMBER_OF_MESSAGES = 10; 72 | 73 | @Autowired 74 | private CountDownLatch messageBarrier; 75 | 76 | @Autowired 77 | private BlockingQueue> receivedMessages; 78 | 79 | @Autowired 80 | private PulsarClient pulsarClient; 81 | 82 | @Autowired 83 | private StreamBridge streamBridge; 84 | 85 | @Test 86 | void testSendingAndReceivingMessages() 87 | throws InterruptedException, PulsarClientException { 88 | 89 | // Send test messages 90 | for (int i = 0; i < NUMBER_OF_MESSAGES; i++) { 91 | streamBridge.send("myEventProducer-out-0", 92 | MessageBuilder.withPayload("Message" + i) 93 | .setHeader("event.eventType", "createEvent").build(), 94 | MimeType.valueOf("text/plain")); 95 | } 96 | 97 | assertThat(this.messageBarrier.await(10, TimeUnit.SECONDS)).isTrue(); 98 | 99 | List> messages = receivedMessages.stream().toList(); 100 | 101 | for (int i = 0; i < NUMBER_OF_MESSAGES; i++) { 102 | Object item = messages.get(i); 103 | assertThat(item).isInstanceOf(GenericMessage.class); 104 | Message message = (Message) item; 105 | assertThat(message.getPayload()).isEqualTo("Message" + i); 106 | assertThat(message.getHeaders()).containsEntry("event.eventType", 107 | "createEvent"); 108 | } 109 | } 110 | 111 | @Configuration 112 | @EnableAutoConfiguration 113 | static class TestConfiguration { 114 | 115 | @Bean 116 | public BlockingQueue> receivedMessages() { 117 | return new ArrayBlockingQueue<>(NUMBER_OF_MESSAGES); 118 | } 119 | 120 | @Bean 121 | public CountDownLatch messageBarrier() { 122 | return new CountDownLatch(NUMBER_OF_MESSAGES); 123 | } 124 | 125 | @Bean 126 | public Consumer> myEventConsumer() { 127 | return message -> { 128 | receivedMessages().add(message); 129 | messageBarrier().countDown(); 130 | }; 131 | } 132 | } 133 | 134 | } 135 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/PulsarProducerMessageHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream; 18 | 19 | import com.datastax.oss.pulsar.springcloudstream.properties.SchemaSpec; 20 | import java.lang.reflect.Field; 21 | import java.util.Map; 22 | import java.util.stream.Collectors; 23 | 24 | import org.apache.pulsar.client.api.Producer; 25 | import org.apache.pulsar.client.api.ProducerBuilder; 26 | import org.apache.pulsar.client.api.PulsarClient; 27 | import org.apache.pulsar.client.api.PulsarClientException; 28 | import org.apache.pulsar.client.api.Schema; 29 | import org.apache.pulsar.client.api.TypedMessageBuilder; 30 | import org.apache.pulsar.client.impl.ProducerBuilderImpl; 31 | import org.apache.pulsar.client.impl.conf.ProducerConfigurationData; 32 | import org.slf4j.Logger; 33 | import org.slf4j.LoggerFactory; 34 | import org.springframework.cloud.stream.binder.ExtendedProducerProperties; 35 | import org.springframework.cloud.stream.provisioning.ProducerDestination; 36 | import org.springframework.context.Lifecycle; 37 | import org.springframework.messaging.Message; 38 | import org.springframework.messaging.MessageChannel; 39 | import org.springframework.messaging.MessageDeliveryException; 40 | import org.springframework.messaging.MessageHandler; 41 | import org.springframework.messaging.MessagingException; 42 | import org.springframework.messaging.converter.MessageConverter; 43 | import org.springframework.messaging.support.ErrorMessage; 44 | import org.springframework.util.ReflectionUtils; 45 | 46 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarProducerProperties; 47 | class PulsarProducerMessageHandler implements MessageHandler, Lifecycle { 48 | private static final Logger LOG = LoggerFactory.getLogger(PulsarProducerMessageHandler.class.getName()); 49 | private static final Field CONF_FIELD = ReflectionUtils.findField( 50 | ProducerBuilderImpl.class, "conf", ProducerConfigurationData.class); 51 | static { 52 | ReflectionUtils.makeAccessible(CONF_FIELD); 53 | } 54 | 55 | private final Producer pulsarProducer; 56 | private final MessageChannel errorChannel; 57 | private final MessageConverter messageConverter; 58 | private volatile boolean running; 59 | 60 | private final boolean useAsyncSend; 61 | 62 | private final SchemaSpec schemaSpec; 63 | 64 | public PulsarProducerMessageHandler(PulsarClient pulsarClient, 65 | ProducerDestination destination, 66 | ExtendedProducerProperties producerProperties, 67 | MessageChannel errorChannel, 68 | MessageConverter messageConverter) { 69 | this.errorChannel = errorChannel; 70 | this.messageConverter = messageConverter; 71 | try { 72 | schemaSpec = producerProperties.getExtension().getSchema(); 73 | ProducerBuilder producerBuilder = pulsarClient.newProducer( 74 | (Schema) schemaSpec.asPulsarSchema()); 75 | // Use reflection since the Pulsar API doesn't have a public way to apply the 76 | // configuration object 77 | ReflectionUtils.setField(CONF_FIELD, producerBuilder, 78 | producerProperties.getExtension().clone()); 79 | pulsarProducer = producerBuilder.topic(destination.getName()).create(); 80 | } 81 | catch (PulsarClientException e) { 82 | throw new RuntimeException(e); 83 | } 84 | this.useAsyncSend = producerProperties.getExtension().isUseSendAsync(); 85 | } 86 | 87 | @Override 88 | public void handleMessage(Message message) throws MessagingException { 89 | Object convertedPayload = messageConverter != null 90 | ? messageConverter.fromMessage(message, schemaSpec.getValueClass()) 91 | : message.getPayload(); 92 | TypedMessageBuilder messageBuilder = pulsarProducer.newMessage() 93 | .value(convertedPayload) 94 | // map headers to Map 95 | .properties(message.getHeaders().entrySet().stream().map(entry -> Map.entry(entry.getKey(), 96 | entry.getValue() != null ? String.valueOf(entry.getValue()) : null)) 97 | .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue))); 98 | if (useAsyncSend) { 99 | messageBuilder.sendAsync() 100 | .exceptionally(throwable -> { 101 | if (errorChannel != null) { 102 | errorChannel.send(new ErrorMessage(throwable, message)); 103 | } else { 104 | // Log send failure if there's no errorChannel 105 | // Producer properties should contain errorChannelEnabled 106 | LOG.warn("Sending message {} failed", message, throwable); 107 | } 108 | return null; 109 | }); 110 | } else { 111 | try { 112 | messageBuilder.send(); 113 | } 114 | catch (PulsarClientException e) { 115 | throw new MessageDeliveryException(message, e); 116 | } 117 | } 118 | } 119 | 120 | @Override 121 | public void start() { 122 | running = true; 123 | } 124 | 125 | @Override 126 | public void stop() { 127 | running = false; 128 | try { 129 | pulsarProducer.close(); 130 | } 131 | catch (PulsarClientException e) { 132 | throw new RuntimeException(e); 133 | } 134 | } 135 | 136 | @Override 137 | public boolean isRunning() { 138 | return running; 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /pulsar-spring-cloud-stream-binder/src/main/java/com/datastax/oss/pulsar/springcloudstream/PulsarMessageChannelBinder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.datastax.oss.pulsar.springcloudstream; 18 | 19 | import java.util.Arrays; 20 | 21 | import org.apache.pulsar.client.api.PulsarClient; 22 | import org.springframework.cloud.stream.binder.AbstractMessageChannelBinder; 23 | import org.springframework.cloud.stream.binder.BinderHeaders; 24 | import org.springframework.cloud.stream.binder.BinderSpecificPropertiesProvider; 25 | import org.springframework.cloud.stream.binder.ExtendedConsumerProperties; 26 | import org.springframework.cloud.stream.binder.ExtendedProducerProperties; 27 | import org.springframework.cloud.stream.binder.ExtendedPropertiesBinder; 28 | import org.springframework.cloud.stream.provisioning.ConsumerDestination; 29 | import org.springframework.cloud.stream.provisioning.ProducerDestination; 30 | import org.springframework.integration.core.MessageProducer; 31 | import org.springframework.messaging.MessageChannel; 32 | import org.springframework.messaging.MessageHandler; 33 | import org.springframework.messaging.converter.MessageConverter; 34 | import org.springframework.util.Assert; 35 | import org.springframework.util.ObjectUtils; 36 | 37 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarBinderConfigurationProperties; 38 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarConsumerProperties; 39 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarExtendedBindingProperties; 40 | import com.datastax.oss.pulsar.springcloudstream.properties.PulsarProducerProperties; 41 | import com.datastax.oss.pulsar.springcloudstream.provisioning.PulsarTopicProvisioner; 42 | 43 | /** 44 | * 45 | * The Spring Cloud Stream Binder implementation for Apache Pulsar. 46 | * 47 | * @author Lari Hotari 48 | * 49 | */ 50 | public class PulsarMessageChannelBinder extends 51 | AbstractMessageChannelBinder, ExtendedProducerProperties, PulsarTopicProvisioner> 52 | implements 53 | ExtendedPropertiesBinder { 54 | 55 | private final PulsarBinderConfigurationProperties configurationProperties; 56 | private final PulsarClient pulsarClient; 57 | private final PulsarExtendedBindingProperties extendedBindingProperties; 58 | 59 | private final MessageConverter messageConverter; 60 | 61 | public PulsarMessageChannelBinder( 62 | PulsarBinderConfigurationProperties configurationProperties, 63 | PulsarTopicProvisioner provisioningProvider, PulsarClient pulsarClient, 64 | PulsarExtendedBindingProperties extendedBindingProperties, MessageConverter messageConverter) { 65 | 66 | super(headersToMap(configurationProperties), provisioningProvider); 67 | this.configurationProperties = configurationProperties; 68 | this.pulsarClient = pulsarClient; 69 | this.extendedBindingProperties = extendedBindingProperties; 70 | this.messageConverter = messageConverter; 71 | } 72 | 73 | @Override 74 | public PulsarConsumerProperties getExtendedConsumerProperties(String channelName) { 75 | return this.extendedBindingProperties.getExtendedConsumerProperties(channelName); 76 | } 77 | 78 | @Override 79 | public PulsarProducerProperties getExtendedProducerProperties(String channelName) { 80 | return this.extendedBindingProperties.getExtendedProducerProperties(channelName); 81 | } 82 | 83 | @Override 84 | public String getDefaultsPrefix() { 85 | return this.extendedBindingProperties.getDefaultsPrefix(); 86 | } 87 | 88 | @Override 89 | public Class getExtendedPropertiesEntryClass() { 90 | return this.extendedBindingProperties.getExtendedPropertiesEntryClass(); 91 | } 92 | 93 | private static String[] headersToMap( 94 | PulsarBinderConfigurationProperties configurationProperties) { 95 | Assert.notNull(configurationProperties, 96 | "'configurationProperties' must not be null"); 97 | if (ObjectUtils.isEmpty(configurationProperties.getHeaders())) { 98 | return BinderHeaders.STANDARD_HEADERS; 99 | } 100 | else { 101 | String[] combinedHeadersToMap = Arrays.copyOfRange( 102 | BinderHeaders.STANDARD_HEADERS, 0, 103 | BinderHeaders.STANDARD_HEADERS.length 104 | + configurationProperties.getHeaders().length); 105 | System.arraycopy(configurationProperties.getHeaders(), 0, 106 | combinedHeadersToMap, BinderHeaders.STANDARD_HEADERS.length, 107 | configurationProperties.getHeaders().length); 108 | return combinedHeadersToMap; 109 | } 110 | } 111 | 112 | @Override 113 | protected MessageHandler createProducerMessageHandler(ProducerDestination destination, 114 | ExtendedProducerProperties producerProperties, 115 | MessageChannel errorChannel) throws Exception { 116 | 117 | return new PulsarProducerMessageHandler(pulsarClient, destination, 118 | producerProperties, errorChannel, messageConverter); 119 | } 120 | 121 | @Override 122 | protected MessageProducer createConsumerEndpoint(ConsumerDestination destination, 123 | String group, 124 | ExtendedConsumerProperties properties) { 125 | return new PulsarConsumerEndpoint(pulsarClient, destination, group, properties); 126 | } 127 | 128 | } 129 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.datastax.oss 7 | pulsar-spring-cloud-stream-binder-parent 8 | 9 | 1.0.0-SNAPSHOT 10 | pom 11 | 12 | 13 | 3.2.3 14 | 17 15 | 2.10.0 16 | 1.16.3 17 | 3.9.0 18 | 2.22.2 19 | 20 | 21 | 22 | pulsar-spring-cloud-stream-binder 23 | 24 | 25 | 26 | 27 | 28 | com.datastax.oss 29 | pulsar-spring-cloud-stream-binder 30 | ${project.version} 31 | 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-stream 36 | ${spring-cloud-stream.version} 37 | 38 | 39 | org.apache.pulsar 40 | pulsar-client 41 | ${pulsar-client.version} 42 | 43 | 44 | org.springframework.cloud 45 | spring-cloud-stream-binder-test 46 | ${spring-cloud-stream.version} 47 | test 48 | 49 | 50 | org.springframework.cloud 51 | spring-cloud-stream-test-support-internal 52 | ${spring-cloud-stream.version} 53 | test 54 | 55 | 56 | 57 | org.testcontainers 58 | testcontainers-bom 59 | ${testcontainers.version} 60 | pom 61 | import 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | org.apache.maven.plugins 73 | maven-antrun-plugin 74 | 75 | 76 | org.apache.maven.plugins 77 | maven-javadoc-plugin 78 | 79 | true 80 | 81 | 82 | 83 | org.apache.maven.plugins 84 | maven-surefire-plugin 85 | 86 | true 87 | 88 | 89 | 90 | 91 | 92 | 93 | org.apache.maven.plugins 94 | maven-compiler-plugin 95 | ${maven-compiler-plugin.version} 96 | 97 | ${java.version} 98 | ${java.version} 99 | -parameters 100 | 101 | 102 | 103 | org.apache.maven.plugins 104 | maven-surefire-plugin 105 | ${maven-surefire-plugin.version} 106 | 107 | 108 | **/*Tests.java 109 | **/*Test.java 110 | 111 | 112 | **/Abstract*.java 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | spring 122 | 123 | 124 | spring-snapshots 125 | Spring Snapshots 126 | https://repo.spring.io/snapshot 127 | 128 | true 129 | 130 | 131 | false 132 | 133 | 134 | 135 | spring-milestones 136 | Spring Milestones 137 | https://repo.spring.io/milestone 138 | 139 | false 140 | 141 | 142 | 143 | spring-releases 144 | Spring Releases 145 | https://repo.spring.io/release 146 | 147 | false 148 | 149 | 150 | 151 | 152 | 153 | spring-snapshots 154 | Spring Snapshots 155 | https://repo.spring.io/snapshot 156 | 157 | true 158 | 159 | 160 | false 161 | 162 | 163 | 164 | spring-milestones 165 | Spring Milestones 166 | https://repo.spring.io/milestone 167 | 168 | false 169 | 170 | 171 | 172 | spring-releases 173 | Spring Releases 174 | https://repo.spring.io/release 175 | 176 | false 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | spring-snapshots 185 | Spring Snapshots 186 | https://repo.spring.io/snapshot 187 | 188 | 189 | spring-milestones 190 | Spring Milestones 191 | https://repo.spring.io/milestone 192 | 193 | 194 | spring-releases 195 | Spring Releases 196 | https://repo.spring.io/release 197 | 198 | 199 | 200 | 201 | spring-snapshots 202 | Spring Snapshots 203 | https://repo.spring.io/snapshot 204 | 205 | 206 | spring-milestones 207 | Spring Milestones 208 | https://repo.spring.io/milestone 209 | 210 | 211 | spring-releases 212 | Spring Releases 213 | https://repo.spring.io/release 214 | 215 | 216 | 217 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------