├── .gitignore ├── LICENSE ├── README.md ├── pom.xml ├── roadmap.md ├── spring-bus-core ├── pom.xml └── src │ ├── main │ ├── java │ │ └── org │ │ │ └── springframework │ │ │ └── bus │ │ │ └── runner │ │ │ ├── EnableMessageBus.java │ │ │ ├── adapter │ │ │ ├── ChannelBinding.java │ │ │ ├── ChannelLocator.java │ │ │ ├── ChannelsMetadata.java │ │ │ ├── DefaultChannelLocator.java │ │ │ ├── Input.java │ │ │ ├── InputChannelBinding.java │ │ │ ├── MessageBusAdapter.java │ │ │ ├── Output.java │ │ │ ├── OutputChannelBinding.java │ │ │ └── discovery │ │ │ │ ├── DiscoveryClientAutoConfiguration.java │ │ │ │ └── DiscoveryClientChannelLocator.java │ │ │ ├── config │ │ │ ├── LifecycleConfiguration.java │ │ │ ├── MessageBusAdapterConfiguration.java │ │ │ ├── MessageBusProperties.java │ │ │ ├── RabbitServiceConfiguration.java │ │ │ └── RedisServiceConfiguration.java │ │ │ └── endpoint │ │ │ └── ChannelsEndpoint.java │ └── resources │ │ └── META-INF │ │ ├── spring-bus │ │ ├── rabbit-bus.properties │ │ └── redis-bus.properties │ │ └── spring.factories │ └── test │ └── java │ └── org │ └── springframework │ └── bus │ └── runner │ ├── adapter │ ├── DefaultChannelLocatorTests.java │ └── DiscoveryClientChannelLocatorTests.java │ └── config │ └── MessageBusAdapterConfigurationTests.java ├── spring-xd-runner ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── springframework │ │ └── bus │ │ └── xd │ │ └── bootstrap │ │ └── ModuleOptionsPropertySourceInitializer.java │ └── resources │ └── META-INF │ └── spring.factories └── spring-xd-samples ├── pom.xml ├── sink ├── pom.xml └── src │ ├── main │ ├── java │ │ ├── config │ │ │ └── ModuleDefinition.java │ │ └── demo │ │ │ └── SinkApplication.java │ └── resources │ │ ├── application.yml │ │ ├── bootstrap.yml │ │ └── config │ │ └── logger.properties │ └── test │ └── java │ └── demo │ └── ModuleApplicationTests.java ├── source-xml ├── pom.xml └── src │ ├── main │ ├── java │ │ └── demo │ │ │ └── ModuleApplication.java │ └── resources │ │ ├── application.yml │ │ ├── bootstrap.yml │ │ └── config │ │ ├── ticker.properties │ │ └── ticker.xml │ └── test │ └── java │ └── demo │ └── ModuleApplicationTests.java ├── source ├── pom.xml └── src │ ├── main │ ├── java │ │ ├── config │ │ │ └── ModuleDefinition.java │ │ └── demo │ │ │ └── SourceApplication.java │ └── resources │ │ ├── application.yml │ │ ├── bootstrap.yml │ │ └── config │ │ └── ticker.properties │ └── test │ └── java │ └── demo │ └── ModuleApplicationTests.java └── tap ├── pom.xml └── src ├── main ├── java │ ├── config │ │ └── ModuleDefinition.java │ └── demo │ │ └── TapApplication.java └── resources │ ├── application.yml │ ├── bootstrap.yml │ └── config │ └── logger.properties └── test └── java └── demo └── ModuleApplicationTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | /application.yml 2 | /application.properties 3 | asciidoctor.css 4 | *~ 5 | .#* 6 | *# 7 | target/ 8 | bin/ 9 | _site/ 10 | .classpath 11 | .project 12 | .settings 13 | .springBeans 14 | .DS_Store 15 | *.sw* 16 | *.iml 17 | *.ipr 18 | *.iws 19 | .idea 20 | .factorypath 21 | spring-xd-samples/*/xd 22 | -------------------------------------------------------------------------------- /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 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring-bus is no longer actively maintained by VMware, Inc. 2 | 3 | # Spring Integration: Messaging as a Microservice 4 | 5 | This is an experimental project allowing a user to develop and run messaging microservices using Spring Integration and run them locally or in the cloud, or even on Spring XD. It also allows a user to develop and run an XD module locally. Just create `MessageChannels` "input" and/or "output" and add `@EnableMessageBus` and run your app as a Spring Boot app (single application context). You just need to connect to the physical broker for the bus, which is automatic if the relevant bus implementation is available on the classpath. The sample uses Redis. 6 | 7 | Here's a sample source module (output channel only): 8 | 9 | ``` 10 | @SpringBootApplication 11 | @EnableMessageBus 12 | @ComponentScan(basePackageClasses=ModuleDefinition.class) 13 | public class ModuleApplication { 14 | 15 | public static void main(String[] args) throws InterruptedException { 16 | SpringApplication.run(ModuleApplication.class, args); 17 | } 18 | 19 | } 20 | 21 | @Configuration 22 | public class ModuleDefinition { 23 | 24 | @Value("${format}") 25 | private String format; 26 | 27 | @Bean 28 | public MessageChannel output() { 29 | return new DirectChannel(); 30 | } 31 | 32 | @Bean 33 | @InboundChannelAdapter(value = "output", autoStartup = "false", poller = @Poller(fixedDelay = "${fixedDelay}", maxMessagesPerPoll = "1")) 34 | public MessageSource timerMessageSource() { 35 | return () -> new GenericMessage<>(new SimpleDateFormat(format).format(new Date())); 36 | } 37 | 38 | } 39 | ``` 40 | 41 | The `bootstrap.yml` has the module group (a.k.a. stream name in XD), name and index, e.g. 42 | 43 | ``` 44 | --- 45 | spring: 46 | bus: 47 | group: testtock 48 | name: ${spring.application.name:ticker} 49 | index: 0 # source 50 | ``` 51 | 52 | To be deployable as an XD module in a "traditional" way you need `/config/*.properties` to point to any available Java config classes (via `base_packages` or `options_class`), or else you can put traditional XML configuration in `/config/*.xml`. You don't need those things to run as a consumer or producer to an existing XD system. There's an XML version of the same sample (a "timer" source). 53 | 54 | ## Multiple Input or Output Channels 55 | 56 | A module can have multiple input or output channels. Instead of just one channel named "input" or "output" you can add multiple `MessageChannel` beans named `input.*` or `output.*` and the names are converted to external channel names on the bus. The external channel names are the "natural" channel name for the module (i.e. `.` or `spring.bus.[input|output]ChannelName` if supplied) plus the `MessageChannel` bean name, period separated. In addition, the bean name can be `input.[queue|topic|tap]:*` or `output.[queue|topic]:*` (i.e. with a channel type as a colon-separated prefix), and the semantics of the external bus channel changes accordingly (a tap is like a topic). For example, you can have two `MessageChannels` called "output" and "output.topic:foo" in a module deployed with "group=bar" and "index=2", and the result is 2 external channels called "bar.2" and "topic:foo.bar.2". 57 | 58 | ## XD Module Samples 59 | 60 | There are several samples, all running on the redis transport (so you need redis running locally to test them): 61 | 62 | * `source` is a Java config version of the classic "timer" module from Spring XD. It has a "fixedDelay" option (in milliseconds) for the period between emitting messages. 63 | 64 | * `sink` is a Java config version of the classic "log" module from Spring XD. It has no options (but some could easily be added), and just logs incoming messages at INFO level. 65 | 66 | * `tap` is the same as the sink sample, except it is configured to tap the source sample output. When it is running it looks a lot like the sink, except that it only gets copies of the messages in the broker, and since it is a pub-sub subscriber, it only gets the messages sent since it started. 67 | 68 | * `source-xml` is a copy of the classic "timer" module from Spring XD. 69 | 70 | If you run the source and the sink and point them at the same redis instance (e.g. do nothing to get the one on localhost, or the one they are both bound to as a service on Cloud Foundry) then they will form a "stream" and start talking to each other. All the samples have friendly JMX and Actuator endpoints for inspecting what is going on in the system. 71 | 72 | ## Module or App 73 | 74 | Code using this library can be deployed as a standalone app or as an XD module. In standalone mode you app will run happily as a service or in any PaaS (Cloud Foundry, Lattice, Heroku, Azure, etc.). Depending on whether your main aim is to develop an XD module and you just want to test it locally using the standalone mode, or if the ultimate goal is a standalone app, there are some things that you might do differently. 75 | 76 | ### Module Options 77 | 78 | Module option (placeholders) default values can be set in `/config/*.properties` as per a normal XD module, and they can be overridden at runtime in standalone mode using standard Spring Boot configuration (e.g. `application.yml`). Because of the way XD likes to organize options, the default values can also be set as `option.*` in `bootstrap.yml` (in standalone mode) or as System properties (generally). 79 | 80 | ### Local Configuration 81 | 82 | The `application.yml` and `bootstrap.yml` files are ignored by XD when deploying the module natively, so you can put whatever you like in there to control the app in standlone mode. 83 | 84 | ### Fat JAR 85 | 86 | You can run in standalone mode from your IDE for testing. To run in production you can create an executable (or "fat") JAR using the standard Spring Boot tooling, but the executable JAR has a load of stuff in it that isn't needed if it's going to be deployed as an XD module. In that case you are better off with the normal JAR packaging provided by Maven or Gradle. 87 | 88 | ## Making Standalone Modules Talk to Each Other 89 | 90 | The "group" and "index" are used to create physical endpoints in the external broker (e.g. `queue..` in Redis), so a source (output only) has `index=0` (the default) and downstream modules have the same group but incremented index, with a sink module (input only) having the highest index. To listen to the output from an existing app, just use the same "group" name and an index 1 larger than the app before it in the chain. The index can be anything, as long as successive modules have consecutive values. 91 | 92 | > Note: since the same naming conventions are used in XD, you can spy on or send messages to an existing XD stream by copying the stream name (to `spring.bus.group`) and knowing the index of the XD module you want to interact with. 93 | 94 | ## Taps 95 | 96 | All output channels are also tapped by default so you can also attach a module to a pub-sub endpoint and listen to the tap if you know the module metadata (e.g. `topic.tap:stream:..` in Redis). To tap an existing output channel you just need to know its group, name and index, e.g. 97 | 98 | ``` 99 | spring: 100 | bus: 101 | group: tocktap 102 | name: logger 103 | index: 0 104 | tap: 105 | group: testtock 106 | name: ticker 107 | index: 0 108 | ``` 109 | 110 | The `spring.bus.tap` section tells the module runner which topic you want to subscribe to. It creates a new group (a tap can't be in the same group as the one it is tapping) and starts a new index count, in case anyone wants to listen downstream. 111 | 112 | ## Build Spring Bus 113 | ### Pre-requisites 114 | 115 | * Required : 116 | * Java 8 117 | * Maven 118 | 119 | Currently the `receptor-client` dependency is not in a public Maven repo. To install it to your local Maven repo, execute the following commands: 120 | 121 | ``` 122 | git clone https://github.com/markfisher/receptor-client.git 123 | cd receptor-client 124 | ./gradlew install 125 | ``` 126 | 127 | ### Building the project 128 | 129 | ``` 130 | mvn clean install 131 | ``` 132 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | org.springframework.bus 5 | spring-bus-parent 6 | 1.0.0.BUILD-SNAPSHOT 7 | pom 8 | http://projects.spring.io/spring-xd/ 9 | 10 | Pivotal Software, Inc. 11 | http://www.spring.io 12 | 13 | 14 | 1.7 15 | 1.3.0.M1 16 | 1.0.3.BUILD-SNAPSHOT 17 | 2.0.0.BUILD-SNAPSHOT 18 | 19 | 20 | spring-bus-core 21 | spring-xd-runner 22 | spring-xd-samples 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-parent 29 | ${spring-boot.version} 30 | pom 31 | import 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-starter-parent 36 | ${spring-cloud.version} 37 | pom 38 | import 39 | 40 | 41 | org.springframework.bus 42 | spring-bus-core 43 | 1.0.0.BUILD-SNAPSHOT 44 | 45 | 46 | org.springframework.bus 47 | spring-xd-runner 48 | 1.0.0.BUILD-SNAPSHOT 49 | 50 | 51 | org.springframework.xd 52 | spring-xd-dirt 53 | ${spring-xd.version} 54 | 55 | 56 | jackson-core-asl 57 | org.codehaus.jackson 58 | 59 | 60 | org.springframework.xd 61 | spring-xd-spark-streaming 62 | 63 | 64 | org.springframework.xd 65 | spring-xd-hadoop 66 | 67 | 68 | org.springframework.xd 69 | spring-xd-batch 70 | 71 | 72 | org.springframework.xd 73 | spring-xd-ui 74 | 75 | 76 | slf4j-log4j12 77 | org.slf4j 78 | 79 | 80 | zookeeper 81 | org.apache.zookeeper 82 | 83 | 84 | spring-boot-starter-security 85 | org.springframework.boot 86 | 87 | 88 | spring-security-ldap 89 | org.springframework.security 90 | 91 | 92 | spring-jdbc 93 | org.springframework 94 | 95 | 96 | spring-batch-integration 97 | org.springframework.batch 98 | 99 | 100 | spring-batch-admin-manager 101 | org.springframework.batch 102 | 103 | 104 | spring-data-mongodb 105 | org.springframework.data 106 | 107 | 108 | 109 | 110 | org.springframework.xd 111 | spring-xd-messagebus-redis 112 | ${spring-xd.version} 113 | 114 | 115 | org.springframework.xd 116 | spring-xd-messagebus-rabbit 117 | ${spring-xd.version} 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | ${basedir}/src/main/resources 126 | true 127 | 128 | **/application.yml 129 | **/application.properties 130 | 131 | 132 | 133 | ${basedir}/src/main/resources 134 | 135 | **/application.yml 136 | **/application.properties 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | org.apache.maven.plugins 145 | maven-compiler-plugin 146 | 147 | ${java.version} 148 | ${java.version} 149 | 150 | 151 | 152 | org.apache.maven.plugins 153 | maven-jar-plugin 154 | 155 | 156 | 157 | ${start-class} 158 | true 159 | 160 | 161 | 162 | 163 | 164 | org.apache.maven.plugins 165 | maven-surefire-plugin 166 | 167 | 168 | **/*Tests.java 169 | **/*Test.java 170 | 171 | 172 | **/Abstract*.java 173 | 174 | 175 | 176 | 177 | org.apache.maven.plugins 178 | maven-war-plugin 179 | 180 | false 181 | 182 | 183 | ${start-class} 184 | true 185 | 186 | 187 | 188 | 189 | 190 | org.codehaus.mojo 191 | exec-maven-plugin 192 | 193 | ${start-class} 194 | 195 | 196 | 197 | org.apache.maven.plugins 198 | maven-resources-plugin 199 | 2.6 200 | 201 | 202 | ${resource.delimiter} 203 | 204 | 205 | 206 | 207 | pl.project13.maven 208 | git-commit-id-plugin 209 | 2.1.11 210 | 211 | 212 | 213 | revision 214 | 215 | 216 | 217 | 218 | true 219 | yyyy-MM-dd'T'HH:mm:ssZ 220 | true 221 | ${project.build.outputDirectory}/git.properties 222 | 223 | 224 | 225 | 226 | org.springframework.boot 227 | spring-boot-maven-plugin 228 | ${spring-boot.version} 229 | 230 | 231 | 232 | repackage 233 | 234 | 235 | 236 | 237 | ${start-class} 238 | 239 | 240 | 241 | 242 | org.apache.maven.plugins 243 | maven-shade-plugin 244 | 245 | 246 | org.springframework.boot 247 | spring-boot-maven-plugin 248 | ${spring-boot.version} 249 | 250 | 251 | 252 | true 253 | true 254 | 255 | 256 | *:* 257 | 258 | META-INF/*.SF 259 | META-INF/*.DSA 260 | META-INF/*.RSA 261 | 262 | 263 | 264 | 265 | 266 | 267 | package 268 | 269 | shade 270 | 271 | 272 | 273 | 274 | META-INF/spring.handlers 275 | 276 | 277 | META-INF/spring.factories 278 | 279 | 280 | META-INF/spring.schemas 281 | 282 | 283 | 284 | ${start-class} 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | spring-snapshots 297 | Spring Snapshots 298 | http://repo.spring.io/snapshot 299 | 300 | true 301 | 302 | 303 | 304 | spring-milestones 305 | Spring Milestones 306 | http://repo.spring.io/milestone 307 | 308 | false 309 | 310 | 311 | 312 | 313 | 314 | spring-snapshots 315 | Spring Snapshots 316 | http://repo.spring.io/snapshot 317 | 318 | true 319 | 320 | 321 | 322 | spring-milestones 323 | Spring Milestones 324 | http://repo.spring.io/milestone 325 | 326 | false 327 | 328 | 329 | 330 | 331 | -------------------------------------------------------------------------------- /roadmap.md: -------------------------------------------------------------------------------- 1 | # Messaging Microservices 2 | 3 | Messaging and asynchronous patterns are completely natural with microservices, but a lot of the state of the art material concentrates on HTTP, JSON and REST. This document is about "Message-driven Microservices" with Spring. It tracks the convergence of various ideas that are floating around in Spring Cloud, Spring Boot and Spring XD. 4 | 5 | This project was originally motivated by the goal of allowing a developer to build and run an XD module locally. We can extrapolate from there to a more flexible model that leads optionally to a deployable XD module, but can also be used to form more flexible structures than a simple "stream". 6 | 7 | ## Basic Programming Model 8 | 9 | Just create `MessageChannels` "input" and/or "output" and add `@EnableMessageBus` and run your app as a Spring Boot app (single application context). You need to connect to the physical broker for the bus, which is automatic if the relevant bus implementation is available on the classpath. The sample uses Redis. 10 | 11 | Here's a sample source module (output channel only): 12 | 13 | ``` 14 | @SpringBootApplication 15 | @EnableMessageBus 16 | @ComponentScan(basePackageClasses=ModuleDefinition.class) 17 | public class MessageBusApplication { 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(MessageBusApplication.class, args); 21 | } 22 | 23 | } 24 | 25 | @Configuration 26 | public class ModuleDefinition { 27 | 28 | @Value("${format}") 29 | private String format; 30 | 31 | @Bean 32 | public MessageChannel output() { 33 | return new DirectChannel(); 34 | } 35 | 36 | @Bean 37 | @InboundChannelAdapter(value = "output", autoStartup = "false", poller = @Poller(fixedDelay = "${fixedDelay}", maxMessagesPerPoll = "1")) 38 | public MessageSource timerMessageSource() { 39 | return () -> new GenericMessage<>(new SimpleDateFormat(format).format(new Date())); 40 | } 41 | 42 | } 43 | ``` 44 | 45 | The `bootstrap.yml` has the module group (a.k.a. stream name), name and index, e.g. 46 | 47 | ``` 48 | --- 49 | spring: 50 | bus: 51 | group: testtock 52 | name: ${spring.application.name:ticker} 53 | index: 0 # source 54 | ``` 55 | 56 | ## Richer Input and Output 57 | 58 | A generic message-driven microservice can have more than 1 input and/or more than 1 output. For example if it contains a router component, then it might want to send messages downstream to 2 (or more) completely different classes of consumers. The number and purpose of those downstream channels might even change at runtime. 59 | 60 | Spring XD has the concept of a "stream" where modules pipe their output into the next module's input (like a UN\*X shell). This is effectively an opinionated, special-case implementation of the more general message-driven microservice. It should be possible to re-architect Spring XD so that it still adds value but builds on top of a framework that is more general. 61 | 62 | Spring XD actually already supports arbitrary graphs of input and output via named channels. One of the aims of this project is to make that easier to program (or declare), and to make modules composable into a self-contained application. 63 | 64 | > NOTE: For the sake of clarity, consider the example of an XD module that contains a router, sending each message to one of two destinations (for invalid or valid messages respectively). For XD purposes this module is a "sink" because it has input but no unique output channel. The output is handled and redirected through the Bus using a `MessageChannelResolver` that is driven by naming convention (destinations start with "queue:" or "topic:" according to their pubsub semantics). Users can create streams that listen to those channels by using the same names. 65 | 66 | ## Deployment as an XD Module 67 | 68 | To be deployable as an XD module in a "traditional" way you need `/config/*.properties` to point to any available Java config classes (via `base_packages` or `options_class`), or else you can put traditional XML configuration in `/config/*.xml`. You don't need those things to run as a consumer or producer to an existing XD system. The `spring-xd-runner` library uses a Spring Cloud bootstrap context to initialize the `Module` environment properties in a way that simulates being deployed in a "full" XD system. 69 | 70 | ## Backlog 71 | 72 | - [x] Support for multiple input and output channels 73 | 74 | - [X] Partitioning 75 | 76 | - [ ] Support channel-level partitioning properties, not just at bus-level 77 | 78 | - [ ] Support for inputType and/or outputType properties 79 | 80 | - [x] Support for pubsub as "primary" input/output (in addition to the existing queue semantics) 81 | 82 | - [x] Endpoint "/channels" for module configuration metadata ("/bus" is taken by Spring Cloud) 83 | 84 | - [x] Discover channel names through Spring Cloud service discovery (via "/channels" endpoint on remote components) 85 | 86 | - [x] Listen for changes in discovery catalog and potentially rebind channels 87 | 88 | - [ ] `@BusClient` like `@FeignClient` where the remote service (and optionally channel) can be specified 89 | 90 | - [ ] Optional validation of application as XD module 91 | 92 | - [ ] Support for more than one `MessageBus` (e.g. local and redis) in the same app 93 | 94 | - [ ] Correlation and message tracing 95 | 96 | - [ ] Extract `spring-xd-dirt` dependencies into a separate module 97 | 98 | - [ ] Re-use existing XD modules as libraries 99 | 100 | - [ ] Re-use existing XD analytics as libraries (possibly attempt merge with Spring Boot metrics) 101 | 102 | - [ ] Support Spring Batch jobs as modules 103 | 104 | ## Barriers to Progress 105 | 106 | The best plan for making progress, where we keep in sight the goal of eventually having Spring XD converge with this project, is to shadow Spring XD and try to extract as much goodness from it as we can. The `MessageBus` is really the core concept and it is already largely split out. 107 | 108 | - [ ] Spring XD package names should eventually be removed as messaging is a lower level concern. 109 | 110 | - [ ] Ditto configuration properties in `xd.*` should be moved to `spring.bus.*` (or something). 111 | 112 | - [ ] We need the XML configuration from `/META-INF/spring-xd/bus/**` and `/META-INF/spring-xd/analytics` but 113 | 114 | - [x] There are no defaults for several properties in `xd.messagebus.*` so applications have to have a load of boilerplate configuration in `application.yml`. Fixed by adding `@PropertySources` to the default configuration. 115 | 116 | - [ ] The "codec.xml" is in `spring-xd-dirt` which we don't want to depend on. Maybe it should be in the messagebus SPI jar? Or we can make a copy and risk it changing in XD. 117 | 118 | - [x] Do we need the analytics configuration? It should at least be optional. Answer "no" (but support for analytics would be cool). 119 | 120 | - [ ] The `spring-xd-dirt` library contains some of the primitives we might need, especially when building the bridge to create XD modules as apps. It would be best if they could be extracted into another library. 121 | 122 | - [ ] `MessageBusAwareChannelResolver` and `MessageBusAwareRouterBeanPostProcessor` should be pulled out of dirt (and dirt should ultimately depend on this project). 123 | 124 | - [x] A `ModuleDefinition` is only needed to initialize options for an XD module (so not really needed for the general case). We can split the module options initializer out into a separate module that you only need if you know you want to test an XD module with its native options metadata. 125 | 126 | - [ ] `XdHeaders` (e.g. for history) 127 | 128 | - [ ] `BusUtils` (e.g. to construct external channel names) 129 | 130 | - [ ] There is a curator dependency in Spring XD that can't be shaken off. 131 | 132 | - [ ] Spring XD plugins provide a rich set of lifecycle hooks, but those would not all be needed and are an awkward mismatch with a "pure-play" Spring Boot approach, where the application is either running or not. 133 | 134 | - [ ] Spring XD Module options are a prime example of the above. The `StreamPlugin` is responsible for setting up default values for "options" which are really nothing more than property sources in the `Environment` (at least from the point of view of the ideal developer experience). Some of the code from that was used in a Spring Cloud bootstrap context in the `spring-xd-runner` project, which puts it in the right part of the lifecycle. It might be better to try and wean XD off its native "options" and back onto a model based on vanilla Spring Boot and Spring Cloud features (`@ConfigurationProperties` and the bootstrap context for example). 135 | -------------------------------------------------------------------------------- /spring-bus-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.bus 7 | spring-bus-core 8 | 1.0.0.BUILD-SNAPSHOT 9 | jar 10 | 11 | spring-bus-runner 12 | Demo project for Spring Integration as apps 13 | 14 | 15 | org.springframework.bus 16 | spring-bus-parent 17 | 1.0.0.BUILD-SNAPSHOT 18 | 19 | 20 | 21 | UTF-8 22 | 1.8 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-actuator 29 | 30 | 31 | org.springframework.cloud 32 | spring-cloud-starter 33 | true 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-web 38 | 39 | 40 | org.springframework.xd 41 | spring-xd-dirt 42 | 43 | 44 | 45 | org.springframework.xd 46 | spring-xd-messagebus-redis 47 | true 48 | 49 | 50 | org.springframework.xd 51 | spring-xd-messagebus-rabbit 52 | true 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-configuration-processor 57 | true 58 | 59 | 60 | 61 | org.springframework.boot 62 | spring-boot-starter-test 63 | test 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/EnableMessageBus.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner; 18 | 19 | import java.lang.annotation.Documented; 20 | import java.lang.annotation.ElementType; 21 | import java.lang.annotation.Inherited; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | import org.springframework.bus.runner.config.LifecycleConfiguration; 27 | import org.springframework.bus.runner.config.MessageBusAdapterConfiguration; 28 | import org.springframework.bus.runner.config.RabbitServiceConfiguration; 29 | import org.springframework.bus.runner.config.RedisServiceConfiguration; 30 | import org.springframework.context.annotation.Configuration; 31 | import org.springframework.context.annotation.Import; 32 | 33 | /** 34 | * @author Dave Syer 35 | * 36 | */ 37 | @Target(ElementType.TYPE) 38 | @Retention(RetentionPolicy.RUNTIME) 39 | @Documented 40 | @Inherited 41 | @Configuration 42 | @Import({ RedisServiceConfiguration.class, RabbitServiceConfiguration.class, 43 | MessageBusAdapterConfiguration.class, LifecycleConfiguration.class }) 44 | public @interface EnableMessageBus { 45 | 46 | } 47 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/ChannelBinding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.adapter; 18 | 19 | /** 20 | * Represents a binding between a local and remote message channel. 21 | * 22 | * @author Dave Syer 23 | * @author Mark Fisher 24 | */ 25 | public abstract class ChannelBinding { 26 | 27 | private String localName; 28 | private String remoteName; 29 | 30 | protected ChannelBinding() { 31 | this(null); 32 | } 33 | 34 | protected ChannelBinding(String localName) { 35 | this.localName = localName; 36 | } 37 | 38 | public String getLocalName() { 39 | return this.localName; 40 | } 41 | 42 | public String getRemoteName() { 43 | return this.remoteName; 44 | } 45 | 46 | public void setRemoteName(String name) { 47 | this.remoteName = name; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/ChannelLocator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.adapter; 18 | 19 | /** 20 | * @author Dave Syer 21 | * 22 | */ 23 | // TODO: Use DestinationResolver? 24 | public interface ChannelLocator { 25 | 26 | String locate(String name); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/ChannelsMetadata.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.adapter; 18 | 19 | import java.util.Collection; 20 | import java.util.Collections; 21 | 22 | import org.springframework.bus.runner.config.MessageBusProperties; 23 | 24 | /** 25 | * @author Dave Syer 26 | */ 27 | public class ChannelsMetadata { 28 | 29 | private Collection outputChannels = Collections.emptySet(); 30 | private Collection inputChannels = Collections.emptySet(); 31 | private MessageBusProperties module; 32 | 33 | public MessageBusProperties getModule() { 34 | return this.module; 35 | } 36 | 37 | public void setModule(MessageBusProperties module) { 38 | this.module = module; 39 | } 40 | 41 | public Collection getOutputChannels() { 42 | return this.outputChannels; 43 | } 44 | 45 | public void setOutputChannels(Collection outputChannels) { 46 | this.outputChannels = outputChannels; 47 | } 48 | 49 | public Collection getInputChannels() { 50 | return this.inputChannels; 51 | } 52 | 53 | public void setInputChannels(Collection inputChannels) { 54 | this.inputChannels = inputChannels; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/DefaultChannelLocator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.adapter; 18 | 19 | import org.springframework.bus.runner.config.MessageBusProperties; 20 | import org.springframework.util.StringUtils; 21 | 22 | /** 23 | * @author Dave Syer 24 | */ 25 | public class DefaultChannelLocator implements ChannelLocator { 26 | 27 | private MessageBusProperties module; 28 | 29 | public DefaultChannelLocator(MessageBusProperties module) { 30 | this.module = module; 31 | } 32 | 33 | @Override 34 | public String locate(String name) { 35 | String channelName = extractChannelName("input", name, this.module.getInputChannelName()); 36 | if (channelName!=null) { 37 | return channelName; 38 | } 39 | channelName = extractChannelName("output", name, this.module.getOutputChannelName()); 40 | if (channelName!=null) { 41 | return channelName; 42 | } 43 | return null; 44 | } 45 | 46 | 47 | private String extractChannelName(String start, String name, String externalChannelName) { 48 | if (name.equals(start)) { 49 | return externalChannelName; 50 | } 51 | else if (name.startsWith(start + ".") || name.startsWith(start + "_")) { 52 | String prefix = ""; 53 | String channelName = name.substring(start.length() + 1); 54 | if (channelName.contains(":")) { 55 | String[] tokens = channelName.split(":", 2); 56 | String type = tokens[0]; 57 | if ("queue".equals(type)) { 58 | // omit the type for a queue 59 | if (StringUtils.hasText(tokens[1])) { 60 | prefix = tokens[1] + "."; 61 | } 62 | } 63 | else { 64 | prefix = channelName + (channelName.endsWith(":") ? "" : "."); 65 | } 66 | } 67 | else { 68 | prefix = channelName + "."; 69 | } 70 | return prefix + getPlainChannelName(externalChannelName); 71 | } 72 | return null; 73 | } 74 | 75 | private String getPlainChannelName(String name) { 76 | if (name.contains(":")) { 77 | name = name.substring(name.indexOf(":") + 1); 78 | } 79 | return name; 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/Input.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.adapter; 18 | 19 | import java.lang.annotation.Documented; 20 | import java.lang.annotation.ElementType; 21 | import java.lang.annotation.Inherited; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | import org.springframework.beans.factory.annotation.Qualifier; 27 | 28 | /** 29 | * Qualifier annotation for a bean relating input channels. 30 | * 31 | * @author Dave Syer 32 | */ 33 | @Qualifier 34 | @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, 35 | ElementType.ANNOTATION_TYPE }) 36 | @Retention(RetentionPolicy.RUNTIME) 37 | @Inherited 38 | @Documented 39 | public @interface Input { 40 | 41 | } 42 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/InputChannelBinding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.adapter; 18 | 19 | /** 20 | * @author Dave Syer 21 | */ 22 | public class InputChannelBinding extends ChannelBinding { 23 | 24 | protected InputChannelBinding() { 25 | super(null); 26 | } 27 | 28 | public InputChannelBinding(String localName) { 29 | super(localName); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/MessageBusAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.adapter; 18 | 19 | import java.util.ArrayList; 20 | import java.util.Collection; 21 | import java.util.Collections; 22 | import java.util.HashMap; 23 | import java.util.LinkedHashMap; 24 | import java.util.LinkedHashSet; 25 | import java.util.Map; 26 | import java.util.Properties; 27 | import java.util.concurrent.atomic.AtomicBoolean; 28 | 29 | import org.slf4j.Logger; 30 | import org.slf4j.LoggerFactory; 31 | import org.springframework.beans.BeansException; 32 | import org.springframework.bus.runner.config.MessageBusProperties; 33 | import org.springframework.context.ApplicationContext; 34 | import org.springframework.context.ApplicationContextAware; 35 | import org.springframework.context.ConfigurableApplicationContext; 36 | import org.springframework.context.Lifecycle; 37 | import org.springframework.integration.channel.ChannelInterceptorAware; 38 | import org.springframework.integration.channel.DirectChannel; 39 | import org.springframework.integration.channel.interceptor.WireTap; 40 | import org.springframework.integration.support.DefaultMessageBuilderFactory; 41 | import org.springframework.integration.support.MessageBuilderFactory; 42 | import org.springframework.integration.support.channel.BeanFactoryChannelResolver; 43 | import org.springframework.jmx.export.annotation.ManagedAttribute; 44 | import org.springframework.jmx.export.annotation.ManagedOperation; 45 | import org.springframework.jmx.export.annotation.ManagedResource; 46 | import org.springframework.messaging.Message; 47 | import org.springframework.messaging.MessageChannel; 48 | import org.springframework.messaging.core.DestinationResolver; 49 | import org.springframework.messaging.support.ChannelInterceptorAdapter; 50 | import org.springframework.util.Assert; 51 | import org.springframework.util.StringUtils; 52 | import org.springframework.xd.dirt.integration.bus.MessageBus; 53 | import org.springframework.xd.dirt.integration.bus.XdHeaders; 54 | 55 | /** 56 | * @author Mark Fisher 57 | * @author Dave Syer 58 | */ 59 | @ManagedResource 60 | public class MessageBusAdapter implements Lifecycle, ApplicationContextAware { 61 | 62 | private static Logger logger = LoggerFactory.getLogger(MessageBusAdapter.class); 63 | 64 | private MessageBus messageBus; 65 | private MessageBuilderFactory messageBuilderFactory = new DefaultMessageBuilderFactory(); 66 | 67 | private Collection outputChannels = Collections.emptySet(); 68 | private Collection inputChannels = Collections.emptySet(); 69 | 70 | private boolean running = false; 71 | 72 | private final AtomicBoolean active = new AtomicBoolean(false); 73 | 74 | private boolean trackHistory = false; 75 | 76 | private MessageBusProperties module; 77 | 78 | private ConfigurableApplicationContext applicationContext; 79 | 80 | private ChannelLocator inputChannelLocator; 81 | 82 | private ChannelLocator outputChannelLocator; 83 | 84 | private DestinationResolver channelResolver; 85 | 86 | private Map bindings = new HashMap(); 87 | 88 | public MessageBusAdapter(MessageBusProperties module, MessageBus messageBus) { 89 | this.module = module; 90 | this.messageBus = messageBus; 91 | this.inputChannelLocator = new DefaultChannelLocator(module); 92 | this.outputChannelLocator = new DefaultChannelLocator(module); 93 | } 94 | 95 | public void setInputChannelLocator(ChannelLocator channelLocator) { 96 | this.inputChannelLocator = channelLocator; 97 | } 98 | 99 | public void setOutputChannelLocator(ChannelLocator channelLocator) { 100 | this.outputChannelLocator = channelLocator; 101 | } 102 | 103 | @Override 104 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 105 | this.applicationContext = (ConfigurableApplicationContext) applicationContext; 106 | this.channelResolver = new BeanFactoryChannelResolver(applicationContext); 107 | } 108 | 109 | public void setChannelResolver(DestinationResolver channelResolver) { 110 | this.channelResolver = channelResolver; 111 | } 112 | 113 | public void setMessageBuilderFactory(MessageBuilderFactory messageBuilderFactory) { 114 | this.messageBuilderFactory = messageBuilderFactory; 115 | } 116 | 117 | public void setTrackHistory(boolean trackHistory) { 118 | this.trackHistory = trackHistory; 119 | } 120 | 121 | public void setOutputChannels(Collection outputChannels) { 122 | this.outputChannels = new LinkedHashSet(outputChannels); 123 | } 124 | 125 | public void setInputChannels(Collection inputChannels) { 126 | this.inputChannels = new LinkedHashSet(inputChannels); 127 | } 128 | 129 | public ChannelsMetadata getChannelsMetadata() { 130 | ChannelsMetadata channels = new ChannelsMetadata(); 131 | channels.setModule(this.module); 132 | channels.setInputChannels(new LinkedHashSet(this.inputChannels)); 133 | channels.setOutputChannels(new LinkedHashSet(this.outputChannels)); 134 | return channels; 135 | } 136 | 137 | public OutputChannelBinding getOutputChannel(String name) { 138 | if (name == null) { 139 | return null; 140 | } 141 | for (OutputChannelBinding binding : this.outputChannels) { 142 | if (name.equals(binding.getRemoteName())) { 143 | return binding; 144 | } 145 | } 146 | for (OutputChannelBinding binding : this.outputChannels) { 147 | if (name.equals(binding.getLocalName())) { 148 | return binding; 149 | } 150 | } 151 | return null; 152 | } 153 | 154 | public InputChannelBinding getInputChannel(String name) { 155 | if (name == null) { 156 | return null; 157 | } 158 | for (InputChannelBinding binding : this.inputChannels) { 159 | if (name.equals(binding.getRemoteName())) { 160 | return binding; 161 | } 162 | } 163 | for (InputChannelBinding binding : this.inputChannels) { 164 | if (name.equals(binding.getLocalName())) { 165 | return binding; 166 | } 167 | } 168 | return null; 169 | } 170 | 171 | public void tap(String outputChannel) { 172 | OutputChannelBinding channel = getOutputChannel(outputChannel); 173 | if (channel == null || channel.isTapped()) { 174 | return; 175 | } 176 | createAndBindTapChannel(channel.getTapChannelName(), channel.getLocalName()); 177 | channel.setTapped(true); 178 | } 179 | 180 | public void untap(String outputChannel) { 181 | OutputChannelBinding channel = getOutputChannel(outputChannel); 182 | if (channel == null || !channel.isTapped()) { 183 | return; 184 | } 185 | String tapChannelName = channel.getTapChannelName(); 186 | this.messageBus.unbindProducers(tapChannelName); 187 | channel.setTapped(false); 188 | } 189 | 190 | @ManagedOperation 191 | public void rebind() { 192 | boolean runnable = locateChannels(); 193 | if (runnable && !this.running) { 194 | start(); 195 | } 196 | if (!runnable && this.running) { 197 | stop(); 198 | } 199 | } 200 | 201 | @Override 202 | @ManagedOperation 203 | public void start() { 204 | if (!this.running) { 205 | // Start everything, but don't call ourselves 206 | if (!this.active.get()) { 207 | if (this.active.compareAndSet(false, true)) { 208 | boolean ready = bindChannels(); 209 | if (ready) { 210 | this.running = true; 211 | this.applicationContext.start(); 212 | } 213 | this.active.set(false); 214 | } 215 | } 216 | } 217 | } 218 | 219 | @Override 220 | @ManagedOperation 221 | public void stop() { 222 | if (this.running) { 223 | if (!this.active.get()) { 224 | if (this.active.compareAndSet(false, true)) { 225 | unbindChannels(); 226 | this.applicationContext.stop(); 227 | this.active.set(false); 228 | } 229 | } 230 | } 231 | this.running = false; 232 | } 233 | 234 | @Override 235 | @ManagedAttribute 236 | public boolean isRunning() { 237 | return this.running && this.applicationContext.isRunning(); 238 | } 239 | 240 | protected final void unbindChannels() { 241 | for (InputChannelBinding binding : this.inputChannels) { 242 | String name = this.bindings.get(binding.getRemoteName()); 243 | if (name == null) { 244 | continue; 245 | } 246 | this.messageBus.unbindConsumers(name); 247 | } 248 | for (OutputChannelBinding binding : this.outputChannels) { 249 | String name = this.bindings.get(binding.getRemoteName()); 250 | if (name == null) { 251 | continue; 252 | } 253 | this.messageBus.unbindProducers(name); 254 | if (binding.isTapped()) { 255 | String tapChannelName = binding.getTapChannelName(); 256 | this.messageBus.unbindProducers(tapChannelName); 257 | } 258 | } 259 | } 260 | 261 | protected final boolean bindChannels() { 262 | if (!locateChannels()) { 263 | return false; 264 | } 265 | Map historyProperties = new LinkedHashMap(); 266 | if (this.trackHistory) { 267 | // TODO: addHistoryTag(); 268 | } 269 | for (OutputChannelBinding binding : this.outputChannels) { 270 | String name = binding.getRemoteName(); 271 | MessageChannel outputChannel = this.channelResolver.resolveDestination(binding.getLocalName()); 272 | bindMessageProducer(outputChannel, name, this.module.getProducerProperties()); 273 | if (binding.isTapped()) { 274 | String tapChannelName = getTapChannelName(name); 275 | binding.setTapChannelName(tapChannelName); 276 | // tappableChannels.put(tapChannelName, outputChannel); 277 | // if (isTapActive(tapChannelName)) { 278 | createAndBindTapChannel(tapChannelName, name); 279 | // } 280 | } 281 | if (this.trackHistory) { 282 | historyProperties.put("outputChannel", name); 283 | track(outputChannel, historyProperties); 284 | } 285 | } 286 | for (InputChannelBinding binding : this.inputChannels) { 287 | String name = binding.getRemoteName(); 288 | MessageChannel inputChannel = this.channelResolver.resolveDestination(binding.getLocalName()); 289 | bindMessageConsumer(inputChannel, name, this.module.getConsumerProperties()); 290 | if (this.trackHistory && this.outputChannels.size() != 1) { 291 | historyProperties.put("inputChannel", name); 292 | track(inputChannel, historyProperties); 293 | } 294 | } 295 | return true; 296 | } 297 | 298 | private boolean locateChannels() { 299 | logger.info("Locating channels"); 300 | boolean located = true; 301 | for (OutputChannelBinding binding : this.outputChannels) { 302 | String name = this.outputChannelLocator.locate(binding.getLocalName()); 303 | if (name == null) { 304 | logger.info("No channel found for: " + binding.getLocalName()); 305 | located = false; 306 | } 307 | binding.setRemoteName(name); 308 | this.bindings.put(binding.getRemoteName(), name); 309 | } 310 | for (InputChannelBinding binding : this.inputChannels) { 311 | String name = this.inputChannelLocator.locate(binding.getLocalName()); 312 | if (name == null) { 313 | logger.info("No channel found for: " + binding.getLocalName()); 314 | located = false; 315 | } 316 | binding.setRemoteName(name); 317 | this.bindings.put(binding.getRemoteName(), name); 318 | } 319 | return located; 320 | } 321 | 322 | // TODO: move this to ChannelLocator? 323 | private String getTapChannelName(String name) { 324 | return !isDefaultOuputChannel(name) ? this.module.getTapChannelName(getPlainChannelName(name)) 325 | : this.module.getTapChannelName(); 326 | } 327 | 328 | // TODO: move this to ChannelLocator? 329 | private String getPlainChannelName(String name) { 330 | if (name.contains(":")) { 331 | name = name.substring(name.indexOf(":") + 1); 332 | } 333 | return name; 334 | } 335 | 336 | // TODO: move this to ChannelLocator? 337 | private boolean isDefaultOuputChannel(String channelName) { 338 | if (channelName.contains(":")) { 339 | String[] tokens = channelName.split(":", 2); 340 | channelName = tokens[1]; 341 | } 342 | return channelName.equals(this.module.getOutputChannelName()); 343 | } 344 | 345 | /* 346 | * Following methods copied from parent to support the bindChannels() method above 347 | */ 348 | 349 | private void bindMessageConsumer(MessageChannel inputChannel, 350 | String inputChannelName, Properties consumerProperties) { 351 | if (isChannelPubSub(inputChannelName)) { 352 | this.messageBus.bindPubSubConsumer(inputChannelName, inputChannel, consumerProperties); 353 | } 354 | else { 355 | this.messageBus.bindConsumer(inputChannelName, inputChannel, consumerProperties); 356 | } 357 | } 358 | 359 | private void bindMessageProducer(MessageChannel outputChannel, 360 | String outputChannelName, Properties producerProperties) { 361 | if (isChannelPubSub(outputChannelName)) { 362 | this.messageBus.bindPubSubProducer(outputChannelName, outputChannel, producerProperties); 363 | } 364 | else { 365 | this.messageBus.bindProducer(outputChannelName, outputChannel, producerProperties); 366 | } 367 | } 368 | 369 | private boolean isChannelPubSub(String channelName) { 370 | Assert.isTrue(StringUtils.hasText(channelName), "Channel name should not be empty/null."); 371 | return (channelName.startsWith("tap:") || channelName.startsWith("topic:")); 372 | } 373 | 374 | /** 375 | * Creates a wiretap on the output channel and binds the tap channel to 376 | * {@link MessageBus}'s message target. 377 | * 378 | * @param tapChannelName the name of the tap channel 379 | * @param localName the channel to tap 380 | */ 381 | private void createAndBindTapChannel(String tapChannelName, String localName) { 382 | logger.info("creating and binding tap channel for {}", tapChannelName); 383 | MessageChannel channel = this.channelResolver.resolveDestination(localName); 384 | if (channel instanceof ChannelInterceptorAware) { 385 | DirectChannel tapChannel = new DirectChannel(); 386 | tapChannel.setBeanName(tapChannelName + ".tap.bridge"); 387 | this.messageBus.bindPubSubProducer(tapChannelName, tapChannel, null); // TODO 388 | // tap 389 | // producer 390 | // props 391 | tapOutputChannel(tapChannel, (ChannelInterceptorAware) channel); 392 | } 393 | else { 394 | if (logger.isDebugEnabled()) { 395 | logger.debug("output channel is not interceptor aware. Tap will not be created."); 396 | } 397 | } 398 | } 399 | 400 | private MessageChannel tapOutputChannel(MessageChannel tapChannel, ChannelInterceptorAware outputChannel) { 401 | outputChannel.addInterceptor(new WireTap(tapChannel)); 402 | return tapChannel; 403 | } 404 | 405 | private void track(MessageChannel channel, final Map historyProps) { 406 | if (channel instanceof ChannelInterceptorAware) { 407 | ((ChannelInterceptorAware) channel) 408 | .addInterceptor(new ChannelInterceptorAdapter() { 409 | 410 | @Override 411 | public Message preSend(Message message, MessageChannel channel) { 412 | @SuppressWarnings("unchecked") 413 | Collection> history = (Collection>) message 414 | .getHeaders().get(XdHeaders.XD_HISTORY); 415 | if (history == null) { 416 | history = new ArrayList>(1); 417 | } 418 | else { 419 | history = new ArrayList>(history); 420 | } 421 | Map map = new LinkedHashMap(); 422 | map.putAll(historyProps); 423 | map.put("thread", Thread.currentThread().getName()); 424 | history.add(map); 425 | Message out = MessageBusAdapter.this.messageBuilderFactory.fromMessage(message) 426 | .setHeader(XdHeaders.XD_HISTORY, history).build(); 427 | return out; 428 | } 429 | }); 430 | } 431 | } 432 | 433 | } 434 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/Output.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.adapter; 18 | 19 | import java.lang.annotation.Documented; 20 | import java.lang.annotation.ElementType; 21 | import java.lang.annotation.Inherited; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | import org.springframework.beans.factory.annotation.Qualifier; 27 | 28 | /** 29 | * Qualifier annotation for a bean relating output channels. 30 | * 31 | * @author Dave Syer 32 | */ 33 | @Qualifier 34 | @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, 35 | ElementType.ANNOTATION_TYPE }) 36 | @Retention(RetentionPolicy.RUNTIME) 37 | @Inherited 38 | @Documented 39 | public @interface Output { 40 | 41 | } 42 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/OutputChannelBinding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.adapter; 18 | 19 | 20 | /** 21 | * @author Dave Syer 22 | */ 23 | public class OutputChannelBinding extends ChannelBinding { 24 | 25 | private boolean tapped = false; 26 | private String tapChannelName; 27 | 28 | protected OutputChannelBinding() { 29 | this(null); 30 | } 31 | 32 | public OutputChannelBinding(String localName) { 33 | super(localName); 34 | } 35 | 36 | public boolean isTapped() { 37 | return this.tapped; 38 | } 39 | 40 | public void setTapped(boolean tapped) { 41 | this.tapped = tapped; 42 | } 43 | 44 | public void setTapChannelName(String tapChannelName) { 45 | this.tapChannelName = tapChannelName; 46 | } 47 | 48 | public String getTapChannelName() { 49 | return this.tapChannelName==null ? "" : this.tapChannelName; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/discovery/DiscoveryClientAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.adapter.discovery; 18 | 19 | import javax.annotation.PostConstruct; 20 | 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 23 | import org.springframework.bus.runner.adapter.ChannelLocator; 24 | import org.springframework.bus.runner.adapter.Output; 25 | import org.springframework.bus.runner.adapter.MessageBusAdapter; 26 | import org.springframework.bus.runner.adapter.Input; 27 | import org.springframework.cloud.client.discovery.DiscoveryClient; 28 | import org.springframework.cloud.client.discovery.event.HeartbeatEvent; 29 | import org.springframework.cloud.client.discovery.event.HeartbeatMonitor; 30 | import org.springframework.context.annotation.Configuration; 31 | import org.springframework.context.event.EventListener; 32 | 33 | /** 34 | * Autoconfiguration for use when user has provided a 35 | * {@link DiscoveryClientChannelLocator}. Listens for changes in the service registry and 36 | * rebinds the external channels as needed. 37 | * 38 | * @author Dave Syer 39 | */ 40 | @Configuration 41 | @ConditionalOnClass(DiscoveryClient.class) 42 | public class DiscoveryClientAutoConfiguration { 43 | 44 | private HeartbeatMonitor monitor = new HeartbeatMonitor(); 45 | 46 | @Autowired 47 | private MessageBusAdapter adapter; 48 | 49 | @Autowired(required = false) 50 | @Input 51 | private ChannelLocator inputChannelLocator; 52 | 53 | @Autowired(required = false) 54 | @Output 55 | private ChannelLocator outputChannelLocator; 56 | 57 | private boolean enabled = false; 58 | 59 | @PostConstruct 60 | public void init() { 61 | if (this.inputChannelLocator instanceof DiscoveryClientChannelLocator 62 | || this.outputChannelLocator instanceof DiscoveryClientChannelLocator) { 63 | this.enabled = true; 64 | } 65 | } 66 | 67 | @EventListener 68 | public void discoveryHeartbeat(HeartbeatEvent event) { 69 | if (this.enabled && this.monitor.update(event.getValue())) { 70 | this.adapter.rebind(); 71 | } 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/discovery/DiscoveryClientChannelLocator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.adapter.discovery; 18 | 19 | import java.net.URI; 20 | import java.util.Collection; 21 | import java.util.Collections; 22 | import java.util.List; 23 | import java.util.Random; 24 | 25 | import org.apache.commons.logging.Log; 26 | import org.apache.commons.logging.LogFactory; 27 | import org.springframework.bus.runner.adapter.ChannelBinding; 28 | import org.springframework.bus.runner.adapter.ChannelLocator; 29 | import org.springframework.bus.runner.adapter.ChannelsMetadata; 30 | import org.springframework.cloud.client.ServiceInstance; 31 | import org.springframework.cloud.client.discovery.DiscoveryClient; 32 | import org.springframework.web.client.RestOperations; 33 | import org.springframework.web.client.RestTemplate; 34 | import org.springframework.web.util.UriComponentsBuilder; 35 | 36 | /** 37 | * @author Dave Syer 38 | */ 39 | public class DiscoveryClientChannelLocator implements ChannelLocator { 40 | 41 | private Log logger = LogFactory.getLog(DiscoveryClientChannelLocator.class); 42 | 43 | private DiscoveryClient discovery; 44 | 45 | private RestOperations restTemplate = new RestTemplate(); 46 | 47 | private String serviceId; 48 | 49 | public DiscoveryClientChannelLocator(DiscoveryClient discovery, String serviceId) { 50 | this.discovery = discovery; 51 | this.serviceId = serviceId; 52 | } 53 | 54 | public void setRestTemplate(RestOperations restTemplate) { 55 | this.restTemplate = restTemplate; 56 | } 57 | 58 | @Override 59 | public String locate(String name) { 60 | List instances = this.discovery.getInstances(this.serviceId); 61 | if (instances == null || instances.isEmpty()) { 62 | return null; 63 | } 64 | URI uri = pickUrl(instances); 65 | try { 66 | ChannelsMetadata channels = this.restTemplate.getForObject(uri, ChannelsMetadata.class); 67 | Collection bindings = Collections.emptySet(); 68 | if (name.startsWith("input")) { 69 | name = name.replace("input", "output"); 70 | bindings = channels.getOutputChannels(); 71 | } 72 | else if (name.startsWith("output")) { 73 | name = name.replace("output", "input"); 74 | bindings = channels.getInputChannels(); 75 | } 76 | for (ChannelBinding binding : bindings) { 77 | if (name.equals(binding.getLocalName())) { 78 | this.logger.debug("Discovered channel for '" + this.serviceId + "' (" 79 | + name + "=" + binding.getRemoteName() + ")"); 80 | return binding.getRemoteName(); 81 | } 82 | } 83 | } 84 | catch (Exception e) { 85 | this.logger.warn("Could not discover channel for '" + this.serviceId + "' (" 86 | + e.getClass() + ": " + e.getMessage() + ")"); 87 | return null; 88 | } 89 | this.logger.warn("No channel disccovered for '" + this.serviceId + "' (" + name + ")"); 90 | return null; 91 | } 92 | 93 | private URI pickUrl(List instances) { 94 | return UriComponentsBuilder 95 | .fromUri(instances.get(new Random().nextInt(instances.size())).getUri()) 96 | .path("channels").build().toUri(); 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/config/LifecycleConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.config; 18 | 19 | import org.springframework.beans.factory.annotation.Autowired; 20 | import org.springframework.boot.CommandLineRunner; 21 | import org.springframework.bus.runner.adapter.MessageBusAdapter; 22 | import org.springframework.context.annotation.Configuration; 23 | 24 | /** 25 | * @author Dave Syer 26 | * 27 | */ 28 | @Configuration 29 | public class LifecycleConfiguration implements CommandLineRunner { 30 | 31 | @Autowired 32 | private MessageBusProperties module; 33 | 34 | @Autowired 35 | private MessageBusAdapter adapter; 36 | 37 | @Override 38 | public void run(String... args) throws Exception { 39 | if (!adapter.isRunning() && module.isAutoStartup()) { 40 | adapter.start(); 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/config/MessageBusAdapterConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 | package org.springframework.bus.runner.config; 17 | 18 | import java.util.Arrays; 19 | import java.util.Collection; 20 | import java.util.LinkedHashSet; 21 | import java.util.Properties; 22 | import java.util.Set; 23 | 24 | import org.aopalliance.intercept.MethodInterceptor; 25 | import org.aopalliance.intercept.MethodInvocation; 26 | import org.springframework.aop.framework.ProxyFactory; 27 | import org.springframework.aop.target.LazyInitTargetSource; 28 | import org.springframework.beans.factory.BeanFactoryUtils; 29 | import org.springframework.beans.factory.ListableBeanFactory; 30 | import org.springframework.beans.factory.annotation.Autowired; 31 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 32 | import org.springframework.bus.runner.adapter.ChannelLocator; 33 | import org.springframework.bus.runner.adapter.Input; 34 | import org.springframework.bus.runner.adapter.InputChannelBinding; 35 | import org.springframework.bus.runner.adapter.MessageBusAdapter; 36 | import org.springframework.bus.runner.adapter.Output; 37 | import org.springframework.bus.runner.adapter.OutputChannelBinding; 38 | import org.springframework.bus.runner.endpoint.ChannelsEndpoint; 39 | import org.springframework.context.annotation.Bean; 40 | import org.springframework.context.annotation.Configuration; 41 | import org.springframework.context.annotation.ImportResource; 42 | import org.springframework.messaging.MessageChannel; 43 | import org.springframework.util.Assert; 44 | import org.springframework.xd.dirt.integration.bus.MessageBus; 45 | import org.springframework.xd.dirt.integration.bus.MessageBusAwareRouterBeanPostProcessor; 46 | 47 | /** 48 | * @author Dave Syer 49 | * 50 | */ 51 | @Configuration 52 | @ImportResource("classpath*:/META-INF/spring-xd/bus/codec.xml") 53 | @EnableConfigurationProperties(MessageBusProperties.class) 54 | public class MessageBusAdapterConfiguration { 55 | 56 | @Autowired 57 | private MessageBusProperties module; 58 | 59 | @Autowired 60 | private ListableBeanFactory beanFactory; 61 | 62 | @Autowired(required=false) 63 | @Input 64 | private ChannelLocator inputChannelLocator; 65 | 66 | @Autowired(required=false) 67 | @Output 68 | private ChannelLocator outputChannelLocator; 69 | 70 | @Bean 71 | public MessageBusAdapter messageBusAdapter(MessageBusProperties module, 72 | MessageBus messageBus) { 73 | MessageBusAdapter adapter = new MessageBusAdapter(module, messageBus); 74 | adapter.setOutputChannels(getOutputChannels()); 75 | adapter.setInputChannels(getInputChannels()); 76 | if (this.inputChannelLocator!=null) { 77 | adapter.setInputChannelLocator(this.inputChannelLocator); 78 | } 79 | if (this.outputChannelLocator!=null) { 80 | adapter.setOutputChannelLocator(this.outputChannelLocator); 81 | } 82 | return adapter; 83 | } 84 | 85 | @Bean 86 | public ChannelsEndpoint channelsEndpoint(MessageBusAdapter adapter) { 87 | return new ChannelsEndpoint(adapter); 88 | } 89 | 90 | protected Collection getOutputChannels() { 91 | Set channels = new LinkedHashSet(); 92 | String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( 93 | this.beanFactory, MessageChannel.class); 94 | for (String name : names) { 95 | if (name.startsWith("output")) { 96 | channels.add(new OutputChannelBinding(name)); 97 | } 98 | } 99 | return channels; 100 | } 101 | 102 | protected Collection getInputChannels() { 103 | Set channels = new LinkedHashSet(); 104 | String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( 105 | this.beanFactory, MessageChannel.class); 106 | for (String name : names) { 107 | if (name.startsWith("input")) { 108 | channels.add(new InputChannelBinding(name)); 109 | } 110 | } 111 | return channels; 112 | } 113 | 114 | // Nested class to avoid instantiating all of the above early 115 | @Configuration 116 | protected static class MessageBusAwareRouterConfiguration { 117 | 118 | @Autowired 119 | private ListableBeanFactory beanFactory; 120 | 121 | @Bean 122 | public MessageBusAwareRouterBeanPostProcessor messageBusAwareRouterBeanPostProcessor() { 123 | 124 | return new MessageBusAwareRouterBeanPostProcessor(createLazyProxy( 125 | this.beanFactory, MessageBus.class), new Properties()); 126 | } 127 | 128 | private T createLazyProxy(ListableBeanFactory beanFactory, Class type) { 129 | ProxyFactory factory = new ProxyFactory(); 130 | LazyInitTargetSource source = new LazyInitTargetSource(); 131 | source.setTargetClass(type); 132 | source.setTargetBeanName(getBeanNameFor(beanFactory, MessageBus.class)); 133 | source.setBeanFactory(beanFactory); 134 | factory.setTargetSource(source); 135 | factory.addAdvice(new PassthruAdvice()); 136 | factory.setInterfaces(new Class[] { type }); 137 | @SuppressWarnings("unchecked") 138 | T proxy = (T) factory.getProxy(); 139 | return proxy; 140 | } 141 | 142 | private String getBeanNameFor(ListableBeanFactory beanFactory, Class type) { 143 | String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( 144 | beanFactory, type, false, false); 145 | Assert.state(names.length == 1, "No unique MessageBus (found " + names.length 146 | + ": " + Arrays.asList(names) + ")"); 147 | return names[0]; 148 | } 149 | 150 | private class PassthruAdvice implements MethodInterceptor { 151 | 152 | @Override 153 | public Object invoke(MethodInvocation invocation) throws Throwable { 154 | return invocation.proceed(); 155 | } 156 | 157 | } 158 | 159 | } 160 | 161 | } 162 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/config/MessageBusProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.config; 18 | 19 | import java.util.Properties; 20 | 21 | import org.springframework.boot.context.properties.ConfigurationProperties; 22 | import org.springframework.util.Assert; 23 | import org.springframework.xd.dirt.integration.bus.BusUtils; 24 | 25 | import com.fasterxml.jackson.annotation.JsonInclude; 26 | import com.fasterxml.jackson.annotation.JsonInclude.Include; 27 | 28 | /** 29 | * @author Dave Syer 30 | * 31 | */ 32 | @ConfigurationProperties("spring.bus") 33 | @JsonInclude(Include.NON_DEFAULT) 34 | public class MessageBusProperties { 35 | 36 | private String name = "module"; 37 | 38 | private String group = "group"; 39 | 40 | private int index = 0; 41 | 42 | private String outputChannelName; 43 | 44 | private String inputChannelName; 45 | 46 | private String type = "processor"; 47 | 48 | private Properties consumerProperties = new Properties(); 49 | 50 | private Properties producerProperties = new Properties(); 51 | 52 | private Tap tap; 53 | 54 | private Discovery discovery = new Discovery(); 55 | 56 | private boolean autoStartup = true; 57 | 58 | public String getName() { 59 | return name; 60 | } 61 | 62 | public void setName(String name) { 63 | this.name = name; 64 | } 65 | 66 | public String getGroup() { 67 | return group; 68 | } 69 | 70 | public void setGroup(String group) { 71 | this.group = group; 72 | } 73 | 74 | public int getIndex() { 75 | return index; 76 | } 77 | 78 | public void setIndex(int index) { 79 | this.index = index; 80 | } 81 | 82 | public String getInputChannelName() { 83 | if (isTap()) { 84 | return String.format("%s.%s.%s", BusUtils.constructTapPrefix(tap.getGroup()), 85 | tap.getName(), tap.getIndex()); 86 | } 87 | return (inputChannelName != null) ? inputChannelName : BusUtils 88 | .constructPipeName(group, index > 0 ? index - 1 : index); 89 | } 90 | 91 | public String getOutputChannelName() { 92 | return (outputChannelName != null) ? outputChannelName : BusUtils 93 | .constructPipeName(group, index); 94 | } 95 | 96 | public String getTapChannelName() { 97 | return getTapChannelName(group); 98 | } 99 | 100 | public String getTapChannelName(String prefix) { 101 | Assert.isTrue(!type.equals("job"), "Job module type not supported."); 102 | // for Stream return channel name with indexed elements 103 | return String 104 | .format("%s.%s.%s", BusUtils.constructTapPrefix(prefix), name, index); 105 | } 106 | 107 | public void setOutputChannelName(String outputChannelName) { 108 | this.outputChannelName = outputChannelName; 109 | } 110 | 111 | public void setInputChannelName(String inputChannelName) { 112 | this.inputChannelName = inputChannelName; 113 | } 114 | 115 | public String getType() { 116 | return type; 117 | } 118 | 119 | public void setType(String type) { 120 | this.type = type; 121 | } 122 | 123 | public Properties getConsumerProperties() { 124 | return consumerProperties; 125 | } 126 | 127 | public void setConsumerProperties(Properties consumerProperties) { 128 | this.consumerProperties = consumerProperties; 129 | } 130 | 131 | public Properties getProducerProperties() { 132 | return producerProperties; 133 | } 134 | 135 | public void setProducerProperties(Properties producerProperties) { 136 | this.producerProperties = producerProperties; 137 | } 138 | 139 | public boolean isAutoStartup() { 140 | return autoStartup; 141 | } 142 | 143 | public void setAutoStartup(boolean autoStartup) { 144 | this.autoStartup = autoStartup; 145 | } 146 | 147 | public Tap getTap() { 148 | return tap; 149 | } 150 | 151 | public void setTap(Tap tap) { 152 | this.tap = tap; 153 | } 154 | 155 | private boolean isTap() { 156 | if (tap != null) { 157 | Assert.state(tap.getName() != null, "Tap name not provided"); 158 | Assert.state(!tap.getGroup().equals(group), 159 | "Tap group cannot be the same as module group"); 160 | } 161 | return tap != null; 162 | } 163 | 164 | public Discovery getDiscovery() { 165 | return discovery; 166 | } 167 | 168 | public static class Discovery { 169 | 170 | private boolean enabled = false; 171 | 172 | private String inputServiceId; 173 | 174 | private String outputServiceId; 175 | 176 | public boolean isEnabled() { 177 | return enabled; 178 | } 179 | 180 | public void setEnabled(boolean enabled) { 181 | this.enabled = enabled; 182 | } 183 | 184 | public String getInputServiceId() { 185 | return inputServiceId; 186 | } 187 | 188 | public void setInputServiceId(String inputServiceId) { 189 | this.inputServiceId = inputServiceId; 190 | } 191 | 192 | public String getOutputServiceId() { 193 | return outputServiceId; 194 | } 195 | 196 | public void setOutputServiceId(String outputServiceId) { 197 | this.outputServiceId = outputServiceId; 198 | } 199 | 200 | } 201 | 202 | public static class Tap { 203 | 204 | private String group = "group"; 205 | 206 | private String name; 207 | 208 | public String getName() { 209 | return name; 210 | } 211 | 212 | public void setName(String name) { 213 | this.name = name; 214 | } 215 | 216 | private int index = 0; 217 | 218 | public String getGroup() { 219 | return group; 220 | } 221 | 222 | public void setGroup(String group) { 223 | this.group = group; 224 | } 225 | 226 | public int getIndex() { 227 | return index; 228 | } 229 | 230 | public void setIndex(int index) { 231 | this.index = index; 232 | } 233 | 234 | } 235 | 236 | } 237 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/config/RabbitServiceConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.config; 18 | 19 | import org.springframework.amqp.rabbit.connection.ConnectionFactory; 20 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 21 | import org.springframework.cloud.Cloud; 22 | import org.springframework.cloud.CloudFactory; 23 | import org.springframework.context.annotation.Bean; 24 | import org.springframework.context.annotation.Configuration; 25 | import org.springframework.context.annotation.ImportResource; 26 | import org.springframework.context.annotation.Profile; 27 | import org.springframework.context.annotation.PropertySource; 28 | import org.springframework.xd.dirt.integration.rabbit.RabbitMessageBus; 29 | 30 | /** 31 | * Bind to services, either locally or in a Lattice environment. 32 | * 33 | * @author Mark Fisher 34 | * @author Dave Syer 35 | */ 36 | @Configuration 37 | @ConditionalOnClass(RabbitMessageBus.class) 38 | @ImportResource({ "classpath*:/META-INF/spring-xd/bus/rabbit-bus.xml", 39 | "classpath*:/META-INF/spring-xd/analytics/rabbit-analytics.xml" }) 40 | @PropertySource("classpath:/META-INF/spring-bus/rabbit-bus.properties") 41 | public class RabbitServiceConfiguration { 42 | @Configuration 43 | @Profile("cloud") 44 | protected static class CloudConfig { 45 | @Bean 46 | public Cloud cloud() { 47 | return new CloudFactory().getCloud(); 48 | } 49 | @Bean 50 | ConnectionFactory redisConnectionFactory(Cloud cloud) { 51 | return cloud.getSingletonServiceConnector(ConnectionFactory.class, null); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/config/RedisServiceConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.config; 18 | 19 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 20 | import org.springframework.cloud.Cloud; 21 | import org.springframework.cloud.CloudFactory; 22 | import org.springframework.context.annotation.Bean; 23 | import org.springframework.context.annotation.Configuration; 24 | import org.springframework.context.annotation.ImportResource; 25 | import org.springframework.context.annotation.Profile; 26 | import org.springframework.context.annotation.PropertySource; 27 | import org.springframework.data.redis.connection.RedisConnectionFactory; 28 | import org.springframework.xd.dirt.integration.redis.RedisMessageBus; 29 | 30 | /** 31 | * Bind to services, either locally or in a Lattice environment. 32 | * 33 | * @author Mark Fisher 34 | * @author Dave Syer 35 | */ 36 | @Configuration 37 | @ConditionalOnClass(RedisMessageBus.class) 38 | @ImportResource({ "classpath*:/META-INF/spring-xd/bus/redis-bus.xml", 39 | "classpath*:/META-INF/spring-xd/analytics/redis-analytics.xml" }) 40 | @PropertySource("classpath:/META-INF/spring-bus/redis-bus.properties") 41 | public class RedisServiceConfiguration { 42 | 43 | @Configuration 44 | @Profile("cloud") 45 | protected static class CloudConfig { 46 | @Bean 47 | public Cloud cloud() { 48 | return new CloudFactory().getCloud(); 49 | } 50 | @Bean 51 | RedisConnectionFactory redisConnectionFactory(Cloud cloud) { 52 | return cloud.getSingletonServiceConnector(RedisConnectionFactory.class, null); 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/java/org/springframework/bus/runner/endpoint/ChannelsEndpoint.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.endpoint; 18 | 19 | import java.util.ArrayList; 20 | import java.util.LinkedHashMap; 21 | import java.util.List; 22 | import java.util.Map; 23 | 24 | import org.springframework.boot.actuate.endpoint.AbstractEndpoint; 25 | import org.springframework.bus.runner.adapter.ChannelsMetadata; 26 | import org.springframework.bus.runner.adapter.MessageBusAdapter; 27 | import org.springframework.bus.runner.adapter.OutputChannelBinding; 28 | import org.springframework.web.bind.annotation.RequestMapping; 29 | import org.springframework.web.bind.annotation.RequestMethod; 30 | import org.springframework.web.bind.annotation.RequestParam; 31 | import org.springframework.web.bind.annotation.RestController; 32 | 33 | /** 34 | * @author Dave Syer 35 | */ 36 | @RestController 37 | public class ChannelsEndpoint extends AbstractEndpoint> { 38 | 39 | private MessageBusAdapter adapter; 40 | 41 | public ChannelsEndpoint(MessageBusAdapter adapter) { 42 | super("channels"); 43 | this.adapter = adapter; 44 | } 45 | 46 | @RequestMapping(value="/channels/taps") 47 | public List taps() { 48 | List list = new ArrayList(); 49 | for (OutputChannelBinding binding : adapter.getChannelsMetadata().getOutputChannels()) { 50 | if (binding.isTapped()) { 51 | list.add(binding); 52 | } 53 | } 54 | return list ; 55 | } 56 | 57 | @RequestMapping(value="/channels/taps", method=RequestMethod.POST) 58 | public OutputChannelBinding tap(@RequestParam String channel) { 59 | adapter.tap(channel); 60 | return adapter.getOutputChannel(channel); 61 | } 62 | 63 | @RequestMapping(value="/channels/taps", method=RequestMethod.DELETE) 64 | public OutputChannelBinding untap(@RequestParam String channel) { 65 | adapter.untap(channel); 66 | return adapter.getOutputChannel(channel); 67 | } 68 | 69 | @Override 70 | public Map invoke() { 71 | LinkedHashMap map = new LinkedHashMap(); 72 | ChannelsMetadata channels = adapter.getChannelsMetadata(); 73 | map.put("inputChannels", channels.getInputChannels()); 74 | map.put("outputChannels", channels.getOutputChannels()); 75 | map.put("module", channels.getModule()); 76 | return map; 77 | } 78 | 79 | } -------------------------------------------------------------------------------- /spring-bus-core/src/main/resources/META-INF/spring-bus/rabbit-bus.properties: -------------------------------------------------------------------------------- 1 | xd.messagebus.rabbit.default.ackMode: AUTO 2 | xd.messagebus.rabbit.default.autoBindDLQ: false 3 | xd.messagebus.rabbit.default.backOffInitialInterval: 1000 4 | xd.messagebus.rabbit.default.backOffMaxInterval: 10000 5 | xd.messagebus.rabbit.default.backOffMultiplier: 2.0 6 | xd.messagebus.rabbit.default.batchBufferLimit: 10000 7 | xd.messagebus.rabbit.default.batchingEnabled: false 8 | xd.messagebus.rabbit.default.batchSize: 100 9 | xd.messagebus.rabbit.default.batchTimeout: 5000 10 | xd.messagebus.rabbit.default.compress: false 11 | xd.messagebus.rabbit.default.concurrency: 1 12 | xd.messagebus.rabbit.default.deliveryMode: PERSISTENT 13 | xd.messagebus.rabbit.default.durableSubscription: false 14 | xd.messagebus.rabbit.default.maxAttempts: 3 15 | xd.messagebus.rabbit.default.maxConcurrency: 1 16 | xd.messagebus.rabbit.default.prefix: xdbus. 17 | xd.messagebus.rabbit.default.prefetch: 1 18 | xd.messagebus.rabbit.default.replyHeaderPatterns: STANDARD_REPLY_HEADERS,* 19 | xd.messagebus.rabbit.default.republishToDLQ: false 20 | xd.messagebus.rabbit.default.requestHeaderPatterns: STANDARD_REQUEST_HEADERS,* 21 | xd.messagebus.rabbit.default.requeue: true 22 | xd.messagebus.rabbit.default.transacted:false 23 | xd.messagebus.rabbit.default.txSize: 1 24 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/resources/META-INF/spring-bus/redis-bus.properties: -------------------------------------------------------------------------------- 1 | xd.messagebus.redis.default.backOffInitialInterval: 1000 2 | xd.messagebus.redis.default.backOffMaxInterval: 10000 3 | xd.messagebus.redis.default.backOffMultiplier: 2.0 4 | xd.messagebus.redis.default.concurrency: 1 5 | xd.messagebus.redis.default.maxAttempts: 3 6 | -------------------------------------------------------------------------------- /spring-bus-core/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # AutoConfiguration 2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 3 | org.springframework.bus.runner.adapter.discovery.DiscoveryClientAutoConfiguration -------------------------------------------------------------------------------- /spring-bus-core/src/test/java/org/springframework/bus/runner/adapter/DefaultChannelLocatorTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 | package org.springframework.bus.runner.adapter; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | 20 | import org.junit.Test; 21 | import org.springframework.bus.runner.config.MessageBusProperties; 22 | 23 | /** 24 | * @author Dave Syer 25 | * 26 | */ 27 | public class DefaultChannelLocatorTests { 28 | 29 | private MessageBusProperties module = new MessageBusProperties(); 30 | 31 | private DefaultChannelLocator locator = new DefaultChannelLocator(this.module); 32 | 33 | @Test 34 | public void oneOutput() throws Exception { 35 | assertEquals("group.0", this.locator.locate("output")); 36 | } 37 | 38 | @Test 39 | public void oneOutputTopic() throws Exception { 40 | assertEquals("topic:group.0", this.locator.locate("output.topic:")); 41 | } 42 | 43 | @Test 44 | public void outputWithNamedTopic() throws Exception { 45 | assertEquals("topic:foo.group.0", this.locator.locate("output.topic:foo")); 46 | } 47 | 48 | @Test 49 | public void outputWithNamedQueue() throws Exception { 50 | assertEquals("foo.group.0", this.locator.locate("output.queue:foo")); 51 | } 52 | 53 | @Test 54 | public void overrideNaturalOutputChannelName() throws Exception { 55 | this.module.setOutputChannelName("bar"); 56 | assertEquals("foo.bar", this.locator.locate("output.queue:foo")); 57 | } 58 | 59 | @Test 60 | public void noQueueQualifier() throws Exception { 61 | assertEquals("foo.group.0", this.locator.locate("output.foo")); 62 | } 63 | 64 | @Test 65 | public void underscoreSeparatorForChannelName() throws Exception { 66 | assertEquals("foo.group.0", this.locator.locate("output_foo")); 67 | } 68 | 69 | @Test 70 | public void overrideNaturalOutputChannelNamedQueue() throws Exception { 71 | this.module.setOutputChannelName("queue:bar"); 72 | assertEquals("foo.bar", this.locator.locate("output.foo")); 73 | } 74 | 75 | @Test 76 | public void overrideNaturalOutputChannelNamedQueueWithTopic() throws Exception { 77 | this.module.setOutputChannelName("queue:bar"); 78 | assertEquals("topic:foo.bar", this.locator.locate("output.topic:foo")); 79 | } 80 | 81 | @Test 82 | public void overrideNaturalOutputChannelNamedTopic() throws Exception { 83 | this.module.setOutputChannelName("topic:bar"); 84 | assertEquals("foo.bar", this.locator.locate("output.queue:foo")); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /spring-bus-core/src/test/java/org/springframework/bus/runner/adapter/DiscoveryClientChannelLocatorTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.adapter; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.mockito.Matchers.any; 21 | 22 | import java.net.URI; 23 | import java.util.Arrays; 24 | import java.util.HashSet; 25 | 26 | import org.junit.Before; 27 | import org.junit.Test; 28 | import org.mockito.Mockito; 29 | import org.springframework.bus.runner.adapter.discovery.DiscoveryClientChannelLocator; 30 | import org.springframework.bus.runner.config.MessageBusProperties; 31 | import org.springframework.cloud.client.DefaultServiceInstance; 32 | import org.springframework.cloud.client.discovery.DiscoveryClient; 33 | import org.springframework.web.client.RestOperations; 34 | 35 | /** 36 | * @author Dave Syer 37 | */ 38 | public class DiscoveryClientChannelLocatorTests { 39 | 40 | private DiscoveryClient client = Mockito.mock(DiscoveryClient.class); 41 | 42 | private RestOperations restTemplate = Mockito.mock(RestOperations.class); 43 | 44 | private DiscoveryClientChannelLocator locator = new DiscoveryClientChannelLocator(this.client, "service"); 45 | 46 | private ChannelsMetadata metadata = new ChannelsMetadata(); 47 | 48 | @Before 49 | public void init() { 50 | this.locator.setRestTemplate(this.restTemplate); 51 | this.metadata.setModule(new MessageBusProperties()); 52 | this.metadata.setInputChannels(new HashSet()); 53 | this.metadata.setOutputChannels(new HashSet()); 54 | Mockito.when( 55 | this.restTemplate.getForObject(Mockito.any(URI.class), anyChannels())) 56 | .thenReturn(this.metadata); 57 | Mockito.when(this.client.getInstances(Mockito.anyString())).thenReturn( 58 | Arrays.asList(new DefaultServiceInstance("service", "example.com", 888, false))); 59 | } 60 | 61 | @Test 62 | public void locateInputFromOutput() { 63 | OutputChannelBinding output = new OutputChannelBinding("output"); 64 | output.setRemoteName("foo.0"); 65 | this.metadata.getOutputChannels().add(output); 66 | assertEquals("foo.0", this.locator.locate("input")); 67 | } 68 | 69 | @Test 70 | public void locateOutputFromInput() { 71 | InputChannelBinding input = new InputChannelBinding("input"); 72 | input.setRemoteName("foo.0"); 73 | this.metadata.getInputChannels().add(input); 74 | assertEquals("foo.0", this.locator.locate("output")); 75 | } 76 | 77 | @SuppressWarnings({ "unchecked" }) 78 | private Class anyChannels() { 79 | return any(Class.class); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /spring-bus-core/src/test/java/org/springframework/bus/runner/config/MessageBusAdapterConfigurationTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.runner.config; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertTrue; 21 | 22 | import java.util.ArrayList; 23 | import java.util.Collection; 24 | import java.util.List; 25 | 26 | import org.junit.Before; 27 | import org.junit.Test; 28 | import org.junit.runner.RunWith; 29 | import org.springframework.beans.factory.annotation.Autowired; 30 | import org.springframework.beans.factory.support.DefaultListableBeanFactory; 31 | import org.springframework.boot.test.SpringApplicationConfiguration; 32 | import org.springframework.bus.runner.adapter.ChannelBinding; 33 | import org.springframework.bus.runner.adapter.MessageBusAdapter; 34 | import org.springframework.bus.runner.adapter.OutputChannelBinding; 35 | import org.springframework.bus.runner.config.MessageBusAdapterConfigurationTests.Empty; 36 | import org.springframework.context.annotation.Bean; 37 | import org.springframework.context.annotation.Configuration; 38 | import org.springframework.context.annotation.Import; 39 | import org.springframework.integration.channel.DirectChannel; 40 | import org.springframework.test.annotation.DirtiesContext; 41 | import org.springframework.test.annotation.DirtiesContext.ClassMode; 42 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 43 | import org.springframework.xd.dirt.integration.bus.local.LocalMessageBus; 44 | 45 | /** 46 | * @author Dave Syer 47 | */ 48 | @RunWith(SpringJUnit4ClassRunner.class) 49 | @SpringApplicationConfiguration(classes = Empty.class) 50 | @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) 51 | public class MessageBusAdapterConfigurationTests { 52 | 53 | @Autowired 54 | private DefaultListableBeanFactory context; 55 | 56 | @Autowired 57 | private MessageBusAdapter adapter; 58 | 59 | @Autowired 60 | private MessageBusAdapterConfiguration configuration; 61 | 62 | @Autowired 63 | private MessageBusProperties module; 64 | 65 | @Before 66 | public void init() { 67 | } 68 | 69 | @Test 70 | public void oneOutput() throws Exception { 71 | this.context.registerSingleton("output", new DirectChannel()); 72 | refresh(); 73 | Collection channels = this.adapter.getChannelsMetadata().getOutputChannels(); 74 | assertEquals(1, channels.size()); 75 | assertEquals("group.0", channels.iterator().next().getRemoteName()); 76 | assertEquals("tap:stream:group.module.0", channels.iterator().next().getTapChannelName()); 77 | } 78 | 79 | private void refresh() { 80 | Collection channels = this.configuration.getOutputChannels(); 81 | for (OutputChannelBinding channel : channels) { 82 | channel.setTapped(true); 83 | } 84 | this.adapter.setOutputChannels(channels); 85 | this.adapter.start(); 86 | } 87 | 88 | @Test 89 | public void oneOutputTopic() throws Exception { 90 | this.context.registerSingleton("output.topic:", new DirectChannel()); 91 | refresh(); 92 | Collection channels = this.adapter.getChannelsMetadata().getOutputChannels(); 93 | assertEquals(1, channels.size()); 94 | assertEquals("topic:group.0", channels.iterator().next().getRemoteName()); 95 | assertEquals("tap:stream:group.module.0", channels.iterator().next().getTapChannelName()); 96 | } 97 | 98 | @Test 99 | public void twoOutputsWithQueue() throws Exception { 100 | this.context.registerSingleton("output", new DirectChannel()); 101 | this.context.registerSingleton("output.queue:foo", new DirectChannel()); 102 | refresh(); 103 | Collection channels = this.adapter.getChannelsMetadata().getOutputChannels(); 104 | List names = getChannelNames(channels); 105 | assertEquals(2, channels.size()); 106 | assertTrue(names.contains("group.0")); 107 | assertTrue(names.contains("foo.group.0")); 108 | } 109 | 110 | private List getChannelNames(Collection channels) { 111 | List list = new ArrayList(); 112 | for (ChannelBinding binding : channels) { 113 | list.add(binding.getRemoteName()); 114 | } 115 | return list; 116 | } 117 | 118 | @Test 119 | public void overrideNaturalOutputChannelName() throws Exception { 120 | this.module.setOutputChannelName("bar"); 121 | this.context.registerSingleton("output.queue:foo", new DirectChannel()); 122 | refresh(); 123 | Collection channels = this.adapter.getChannelsMetadata().getOutputChannels(); 124 | assertEquals(1, channels.size()); 125 | assertEquals("foo.bar", channels.iterator().next().getRemoteName()); 126 | // TODO: fix this. What should it be? 127 | assertEquals("tap:stream:foo.bar.module.0", channels.iterator().next().getTapChannelName()); 128 | } 129 | 130 | @Test 131 | public void overrideNaturalOutputChannelNamedQueueWithTopic() throws Exception { 132 | this.module.setOutputChannelName("queue:bar"); 133 | this.context.registerSingleton("output.topic:foo", new DirectChannel()); 134 | refresh(); 135 | Collection channels = this.adapter.getChannelsMetadata().getOutputChannels(); 136 | assertEquals(1, channels.size()); 137 | assertEquals("topic:foo.bar", channels.iterator().next().getRemoteName()); 138 | assertEquals("tap:stream:foo.bar.module.0", channels.iterator().next().getTapChannelName()); 139 | } 140 | 141 | @Configuration 142 | @Import(MessageBusAdapterConfiguration.class) 143 | protected static class Empty { 144 | @Bean 145 | public LocalMessageBus messageBus() { 146 | return new LocalMessageBus(); 147 | } 148 | } 149 | 150 | } 151 | -------------------------------------------------------------------------------- /spring-xd-runner/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.bus 7 | spring-xd-runner 8 | 1.0.0.BUILD-SNAPSHOT 9 | jar 10 | 11 | spring-xd-module-runner 12 | Demo project for Spring XD Modules as apps 13 | 14 | 15 | org.springframework.bus 16 | spring-bus-parent 17 | 1.0.0.BUILD-SNAPSHOT 18 | 19 | 20 | 21 | UTF-8 22 | 1.8 23 | 24 | 25 | 26 | 27 | org.springframework.cloud 28 | spring-cloud-context 29 | 30 | 31 | org.springframework.bus 32 | spring-bus-core 33 | 34 | 35 | org.springframework.xd 36 | spring-xd-dirt 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /spring-xd-runner/src/main/java/org/springframework/bus/xd/bootstrap/ModuleOptionsPropertySourceInitializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 org.springframework.bus.xd.bootstrap; 18 | 19 | import java.net.URL; 20 | import java.net.URLClassLoader; 21 | import java.util.ArrayList; 22 | import java.util.LinkedHashMap; 23 | import java.util.List; 24 | import java.util.Map; 25 | 26 | import org.springframework.beans.factory.annotation.Autowired; 27 | import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; 28 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 29 | import org.springframework.bus.runner.config.MessageBusProperties; 30 | import org.springframework.context.ApplicationContextInitializer; 31 | import org.springframework.context.ConfigurableApplicationContext; 32 | import org.springframework.context.annotation.Bean; 33 | import org.springframework.context.annotation.Configuration; 34 | import org.springframework.core.Ordered; 35 | import org.springframework.core.annotation.Order; 36 | import org.springframework.core.env.ConfigurableEnvironment; 37 | import org.springframework.core.env.Environment; 38 | import org.springframework.core.env.MapPropertySource; 39 | import org.springframework.xd.dirt.plugins.job.JobPluginMetadataResolver; 40 | import org.springframework.xd.dirt.plugins.stream.ModuleTypeConversionPluginMetadataResolver; 41 | import org.springframework.xd.module.ModuleDefinition; 42 | import org.springframework.xd.module.ModuleDefinitions; 43 | import org.springframework.xd.module.ModuleType; 44 | import org.springframework.xd.module.options.DefaultModuleOptionsMetadataResolver; 45 | import org.springframework.xd.module.options.DelegatingModuleOptionsMetadataResolver; 46 | import org.springframework.xd.module.options.EnvironmentAwareModuleOptionsMetadataResolver; 47 | import org.springframework.xd.module.options.ModuleOption; 48 | import org.springframework.xd.module.options.ModuleOptionsMetadata; 49 | import org.springframework.xd.module.options.ModuleOptionsMetadataResolver; 50 | 51 | /** 52 | * Initialize the application context with default values for the module options. 53 | * 54 | * @author Dave Syer 55 | * 56 | */ 57 | @Configuration 58 | @EnableConfigurationProperties(MessageBusProperties.class) 59 | @Order(Ordered.HIGHEST_PRECEDENCE + 10) 60 | public class ModuleOptionsPropertySourceInitializer implements 61 | ApplicationContextInitializer { 62 | 63 | @Autowired 64 | private MessageBusProperties module = new MessageBusProperties(); 65 | 66 | @Autowired(required=false) 67 | private EnvironmentAwareModuleOptionsMetadataResolver wrapper; 68 | 69 | @Override 70 | public void initialize(ConfigurableApplicationContext applicationContext) { 71 | ConfigurableEnvironment environment = applicationContext.getEnvironment(); 72 | ModuleOptionsMetadataResolver resolver = moduleOptionsMetadataResolver(environment); 73 | ModuleOptionsMetadata resolved = resolver.resolve(getModuleDefinition(applicationContext)); 74 | Map map = new LinkedHashMap(); 75 | for (ModuleOption option : resolved) { 76 | if (option.getDefaultValue() != null) { 77 | map.put(option.getName(), option.getDefaultValue()); 78 | } 79 | } 80 | insert(environment, new MapPropertySource("moduleDefaults", map)); 81 | } 82 | 83 | private ModuleDefinition getModuleDefinition(ConfigurableApplicationContext applicationContext) { 84 | String location = "file:."; 85 | ClassLoader classLoader = applicationContext.getClassLoader(); 86 | if (classLoader instanceof URLClassLoader) { 87 | URL[] urls = ((URLClassLoader) classLoader).getURLs(); 88 | String firstUrl = urls[0].toString(); 89 | if (firstUrl.startsWith("jar:") && firstUrl.endsWith("!/")) { 90 | location = firstUrl.substring(4, firstUrl.length() - 2); 91 | } 92 | } 93 | return ModuleDefinitions.simple(module.getName(), 94 | ModuleType.valueOf(module.getType()), location); 95 | } 96 | 97 | private void insert(ConfigurableEnvironment environment, MapPropertySource source) { 98 | environment.getPropertySources().addLast(source); 99 | } 100 | 101 | private ModuleOptionsMetadataResolver moduleOptionsMetadataResolver(Environment environment) { 102 | List delegates = new ArrayList(); 103 | delegates.add(defaultResolver()); 104 | delegates.add(new ModuleTypeConversionPluginMetadataResolver()); 105 | delegates.add(new JobPluginMetadataResolver()); 106 | DelegatingModuleOptionsMetadataResolver delegatingResolver = new DelegatingModuleOptionsMetadataResolver(); 107 | delegatingResolver.setDelegates(delegates); 108 | ModuleOptionsMetadataResolver resolver = delegatingResolver; 109 | if (wrapper!=null) { 110 | wrapper.setDelegate(delegatingResolver); 111 | resolver = wrapper; 112 | } 113 | return resolver; 114 | } 115 | 116 | @Bean 117 | // TODO: allow override of this 118 | public DefaultModuleOptionsMetadataResolver defaultResolver() { 119 | DefaultModuleOptionsMetadataResolver defaultResolver = new DefaultModuleOptionsMetadataResolver(); 120 | defaultResolver.setShouldCreateModuleClassLoader(false); 121 | return defaultResolver; 122 | } 123 | 124 | @ConditionalOnExpression("'${xd.module.config.location:${xd.config.home:}}'!=''") 125 | protected static class EnvironmentAwareModuleOptionsMetadataResolverConfiguration { 126 | @Bean 127 | public EnvironmentAwareModuleOptionsMetadataResolver environmentAwareModuleOptionsMetadataResolver() { 128 | return new EnvironmentAwareModuleOptionsMetadataResolver(); 129 | } 130 | } 131 | 132 | } 133 | -------------------------------------------------------------------------------- /spring-xd-runner/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.cloud.bootstrap.BootstrapConfiguration:\ 2 | org.springframework.bus.xd.bootstrap.ModuleOptionsPropertySourceInitializer -------------------------------------------------------------------------------- /spring-xd-samples/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | org.springframework.bus 5 | spring-xd-samples 6 | 1.0.0.BUILD-SNAPSHOT 7 | pom 8 | http://projects.spring.io/spring-xd/ 9 | 10 | Pivotal Software, Inc. 11 | http://www.spring.io 12 | 13 | 14 | org.springframework.bus 15 | spring-bus-parent 16 | 1.0.0.BUILD-SNAPSHOT 17 | 18 | 19 | source 20 | sink 21 | tap 22 | source-xml 23 | 24 | 25 | 26 | 27 | 28 | 29 | maven-deploy-plugin 30 | 31 | true 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /spring-xd-samples/sink/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.bus 7 | spring-xd-module-runner-sample-sink 8 | 1.0.0.BUILD-SNAPSHOT 9 | jar 10 | 11 | spring-xd-module-runner-sample-sink 12 | Demo project for Spring XD module 13 | 14 | 15 | org.springframework.bus 16 | spring-xd-samples 17 | 1.0.0.BUILD-SNAPSHOT 18 | 19 | 20 | 21 | UTF-8 22 | demo.SinkApplication 23 | 1.8 24 | 25 | 26 | 27 | 28 | org.springframework.bus 29 | spring-xd-runner 30 | 31 | 32 | org.springframework.xd 33 | spring-xd-messagebus-redis 34 | 35 | 36 | org.springframework.cloud 37 | spring-cloud-lattice-connector 38 | 1.0.2.BUILD-SNAPSHOT 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-configuration-processor 43 | true 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-test 49 | test 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /spring-xd-samples/sink/src/main/java/config/ModuleDefinition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 config; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | import org.springframework.context.annotation.Bean; 22 | import org.springframework.context.annotation.Configuration; 23 | import org.springframework.integration.annotation.MessageEndpoint; 24 | import org.springframework.integration.annotation.ServiceActivator; 25 | import org.springframework.integration.channel.DirectChannel; 26 | import org.springframework.messaging.MessageChannel; 27 | 28 | /** 29 | * @author Dave Syer 30 | * 31 | */ 32 | @Configuration 33 | @MessageEndpoint 34 | public class ModuleDefinition { 35 | 36 | private static Logger logger = LoggerFactory.getLogger(ModuleDefinition.class); 37 | 38 | @Bean 39 | public MessageChannel input() { 40 | return new DirectChannel(); 41 | } 42 | 43 | @ServiceActivator(inputChannel="input") 44 | public void loggerSink(Object payload) { 45 | logger.info("Received: " + payload); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /spring-xd-samples/sink/src/main/java/demo/SinkApplication.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.bus.runner.EnableMessageBus; 6 | import org.springframework.context.annotation.ComponentScan; 7 | 8 | import config.ModuleDefinition; 9 | 10 | @SpringBootApplication 11 | @EnableMessageBus 12 | @ComponentScan(basePackageClasses=ModuleDefinition.class) 13 | public class SinkApplication { 14 | 15 | public static void main(String[] args) throws InterruptedException { 16 | SpringApplication.run(SinkApplication.class, args); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /spring-xd-samples/sink/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8081 3 | -------------------------------------------------------------------------------- /spring-xd-samples/sink/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | --- 2 | spring: 3 | bus: 4 | group: testtock 5 | name: logger 6 | index: 1 7 | # uncomment below to have this module consume from a specific partition 8 | # in the range of 0 to N-1, where N is the upstream module's partitionCount 9 | #consumerProperties: 10 | # partitionIndex: 1 11 | -------------------------------------------------------------------------------- /spring-xd-samples/sink/src/main/resources/config/logger.properties: -------------------------------------------------------------------------------- 1 | base_packages = config -------------------------------------------------------------------------------- /spring-xd-samples/sink/src/test/java/demo/ModuleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.test.annotation.DirtiesContext; 6 | import org.springframework.test.context.web.WebAppConfiguration; 7 | import org.springframework.boot.test.SpringApplicationConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | 10 | @RunWith(SpringJUnit4ClassRunner.class) 11 | @SpringApplicationConfiguration(classes = SinkApplication.class) 12 | @WebAppConfiguration 13 | @DirtiesContext 14 | public class ModuleApplicationTests { 15 | 16 | @Test 17 | public void contextLoads() { 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /spring-xd-samples/source-xml/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.bus 7 | spring-xd-module-runner-sample-source-xml 8 | 1.0.0.BUILD-SNAPSHOT 9 | jar 10 | 11 | spring-xd-module-runner-sample-source-xml 12 | Demo project for Spring XD module 13 | 14 | 15 | org.springframework.bus 16 | spring-xd-samples 17 | 1.0.0.BUILD-SNAPSHOT 18 | 19 | 20 | 21 | UTF-8 22 | demo.ModuleApplication 23 | 1.8 24 | 25 | 26 | 27 | 28 | org.springframework.bus 29 | spring-xd-runner 30 | 31 | 32 | org.springframework.xd 33 | spring-xd-messagebus-redis 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-configuration-processor 38 | true 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-xd-samples/source-xml/src/main/java/demo/ModuleApplication.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.boot.builder.SpringApplicationBuilder; 5 | import org.springframework.bus.runner.EnableMessageBus; 6 | import org.springframework.context.annotation.ImportResource; 7 | import org.springframework.context.annotation.PropertySource; 8 | 9 | @SpringBootApplication 10 | @EnableMessageBus 11 | @ImportResource("classpath:/config/ticker.xml") 12 | @PropertySource("classpath:/config/ticker.properties") 13 | public class ModuleApplication { 14 | 15 | public static void main(String[] args) throws InterruptedException { 16 | new SpringApplicationBuilder().sources(ModuleApplication.class).run(args); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /spring-xd-samples/source-xml/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | fixedDelay: 5 2 | -------------------------------------------------------------------------------- /spring-xd-samples/source-xml/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | --- 2 | spring: 3 | bus: 4 | group: testtock 5 | name: ticker 6 | -------------------------------------------------------------------------------- /spring-xd-samples/source-xml/src/main/resources/config/ticker.properties: -------------------------------------------------------------------------------- 1 | options_class = org.springframework.xd.dirt.modules.metadata.TimeSourceOptionsMetadata -------------------------------------------------------------------------------- /spring-xd-samples/source-xml/src/main/resources/config/ticker.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /spring-xd-samples/source-xml/src/test/java/demo/ModuleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.test.annotation.DirtiesContext; 6 | import org.springframework.test.context.web.WebAppConfiguration; 7 | import org.springframework.boot.test.SpringApplicationConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | 10 | @RunWith(SpringJUnit4ClassRunner.class) 11 | @SpringApplicationConfiguration(classes = ModuleApplication.class) 12 | @WebAppConfiguration 13 | @DirtiesContext 14 | public class ModuleApplicationTests { 15 | 16 | @Test 17 | public void contextLoads() { 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /spring-xd-samples/source/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.bus 7 | spring-xd-module-runner-sample-source 8 | 1.0.0.BUILD-SNAPSHOT 9 | jar 10 | 11 | spring-xd-module-runner-sample-source 12 | Demo project for Spring XD module 13 | 14 | 15 | org.springframework.bus 16 | spring-xd-samples 17 | 1.0.0.BUILD-SNAPSHOT 18 | 19 | 20 | 21 | UTF-8 22 | demo.SourceApplication 23 | 1.8 24 | 25 | 26 | 27 | 28 | org.springframework.bus 29 | spring-xd-runner 30 | 31 | 32 | org.springframework.xd 33 | spring-xd-messagebus-redis 34 | 35 | 36 | org.springframework.cloud 37 | spring-cloud-lattice-connector 38 | 1.0.2.BUILD-SNAPSHOT 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-configuration-processor 43 | true 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-maven-plugin 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /spring-xd-samples/source/src/main/java/config/ModuleDefinition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 config; 18 | 19 | import java.text.SimpleDateFormat; 20 | import java.util.Date; 21 | 22 | import org.springframework.beans.factory.annotation.Value; 23 | import org.springframework.context.annotation.Bean; 24 | import org.springframework.context.annotation.Configuration; 25 | import org.springframework.integration.annotation.InboundChannelAdapter; 26 | import org.springframework.integration.annotation.Poller; 27 | import org.springframework.integration.channel.DirectChannel; 28 | import org.springframework.integration.core.MessageSource; 29 | import org.springframework.messaging.MessageChannel; 30 | import org.springframework.messaging.support.GenericMessage; 31 | 32 | /** 33 | * @author Dave Syer 34 | * 35 | */ 36 | @Configuration 37 | public class ModuleDefinition { 38 | 39 | @Value("${format}") 40 | private String format; 41 | 42 | @Bean 43 | public MessageChannel output() { 44 | return new DirectChannel(); 45 | } 46 | 47 | @Bean 48 | @InboundChannelAdapter(value = "output", autoStartup = "false", poller = @Poller(fixedDelay = "${fixedDelay}", maxMessagesPerPoll = "1")) 49 | public MessageSource timerMessageSource() { 50 | return () -> new GenericMessage<>(new SimpleDateFormat(format).format(new Date())); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /spring-xd-samples/source/src/main/java/demo/SourceApplication.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.bus.runner.EnableMessageBus; 6 | import org.springframework.context.annotation.ComponentScan; 7 | 8 | import config.ModuleDefinition; 9 | 10 | @SpringBootApplication 11 | @EnableMessageBus 12 | @ComponentScan(basePackageClasses=ModuleDefinition.class) 13 | public class SourceApplication { 14 | 15 | public static void main(String[] args) throws InterruptedException { 16 | SpringApplication.run(SourceApplication.class, args); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /spring-xd-samples/source/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | fixedDelay: 5000 2 | -------------------------------------------------------------------------------- /spring-xd-samples/source/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | --- 2 | spring: 3 | bus: 4 | group: testtock 5 | name: ticker 6 | # uncomment below to use the last digit of the seconds as a partition key 7 | # hashcode(key) % N is then applied with N being the partitionCount value 8 | # thus, even seconds should go to the 0 queue, odd seconds to the 1 queue 9 | #producerProperties: 10 | # partitionKeyExpression: payload.charAt(payload.length()-1) 11 | # partitionCount: 2 12 | -------------------------------------------------------------------------------- /spring-xd-samples/source/src/main/resources/config/ticker.properties: -------------------------------------------------------------------------------- 1 | options_class = org.springframework.xd.dirt.modules.metadata.TimeSourceOptionsMetadata 2 | base_packages = config -------------------------------------------------------------------------------- /spring-xd-samples/source/src/test/java/demo/ModuleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.test.annotation.DirtiesContext; 6 | import org.springframework.test.context.web.WebAppConfiguration; 7 | import org.springframework.boot.test.SpringApplicationConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | 10 | @RunWith(SpringJUnit4ClassRunner.class) 11 | @SpringApplicationConfiguration(classes = SourceApplication.class) 12 | @WebAppConfiguration 13 | @DirtiesContext 14 | public class ModuleApplicationTests { 15 | 16 | @Test 17 | public void contextLoads() { 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /spring-xd-samples/tap/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.xd 7 | spring-xd-module-runner-sample-tap 8 | 1.0.0.BUILD-SNAPSHOT 9 | jar 10 | 11 | spring-xd-module-runner-sample-tap 12 | Demo project for Spring XD module 13 | 14 | 15 | org.springframework.bus 16 | spring-xd-samples 17 | 1.0.0.BUILD-SNAPSHOT 18 | 19 | 20 | 21 | UTF-8 22 | demo.TapApplication 23 | 1.8 24 | 25 | 26 | 27 | 28 | org.springframework.bus 29 | spring-xd-runner 30 | 31 | 32 | org.springframework.xd 33 | spring-xd-messagebus-redis 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-configuration-processor 38 | true 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-xd-samples/tap/src/main/java/config/ModuleDefinition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 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 | * http://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 config; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | import org.springframework.context.annotation.Bean; 22 | import org.springframework.context.annotation.Configuration; 23 | import org.springframework.integration.annotation.MessageEndpoint; 24 | import org.springframework.integration.annotation.ServiceActivator; 25 | import org.springframework.integration.channel.DirectChannel; 26 | import org.springframework.messaging.MessageChannel; 27 | 28 | /** 29 | * @author Dave Syer 30 | * 31 | */ 32 | @Configuration 33 | @MessageEndpoint 34 | public class ModuleDefinition { 35 | 36 | private static Logger logger = LoggerFactory.getLogger(ModuleDefinition.class); 37 | 38 | @Bean 39 | public MessageChannel input() { 40 | return new DirectChannel(); 41 | } 42 | 43 | @ServiceActivator(inputChannel="input") 44 | public void loggerSink(Object payload) { 45 | logger.info("Received: " + payload); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /spring-xd-samples/tap/src/main/java/demo/TapApplication.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.bus.runner.EnableMessageBus; 6 | import org.springframework.context.annotation.ComponentScan; 7 | 8 | import config.ModuleDefinition; 9 | 10 | @SpringBootApplication 11 | @EnableMessageBus 12 | @ComponentScan(basePackageClasses=ModuleDefinition.class) 13 | public class TapApplication { 14 | 15 | public static void main(String[] args) throws InterruptedException { 16 | SpringApplication.run(TapApplication.class, args); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /spring-xd-samples/tap/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8081 3 | -------------------------------------------------------------------------------- /spring-xd-samples/tap/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | --- 2 | spring: 3 | bus: 4 | group: tocktap 5 | name: logger 6 | index: 0 7 | tap: 8 | group: testtock 9 | name: ticker 10 | index: 0 11 | -------------------------------------------------------------------------------- /spring-xd-samples/tap/src/main/resources/config/logger.properties: -------------------------------------------------------------------------------- 1 | base_packages = config -------------------------------------------------------------------------------- /spring-xd-samples/tap/src/test/java/demo/ModuleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.test.annotation.DirtiesContext; 6 | import org.springframework.test.context.web.WebAppConfiguration; 7 | import org.springframework.boot.test.SpringApplicationConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | 10 | @RunWith(SpringJUnit4ClassRunner.class) 11 | @SpringApplicationConfiguration(classes = TapApplication.class) 12 | @WebAppConfiguration 13 | @DirtiesContext 14 | public class ModuleApplicationTests { 15 | 16 | @Test 17 | public void contextLoads() { 18 | } 19 | 20 | } 21 | --------------------------------------------------------------------------------