├── .gitignore ├── .travis.yml ├── CODE-OF-CONDUCT.md ├── GOVERNANCE.md ├── LICENSE ├── MAINTAINERS.md ├── README.md ├── pom.xml └── src ├── main └── java │ └── io │ └── nats │ └── connector │ ├── plugins │ └── redis │ │ └── RedisPubSubPlugin.java │ └── redis │ └── RedisPubSubConnector.java └── test └── java └── io └── nats └── connector └── plugins └── redis ├── NATSServer.java ├── RedisPubSubPluginTest.java ├── TestCasePrinterRule.java ├── UnitTest.java └── UnitTestUtilities.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Eclipse stuff 2 | .checkstyle 3 | .classpath 4 | .project 5 | .settings/ 6 | 7 | # IntelliJ stuff 8 | .idea 9 | *.iml 10 | 11 | # Build artifacts 12 | *.class 13 | target/ 14 | bin/ 15 | 16 | # Mobile Tools for Java (J2ME) 17 | .mtj.tmp/ 18 | 19 | # Package Files # 20 | *.jar 21 | *.war 22 | *.ear 23 | 24 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 25 | hs_err_pid* 26 | /target/ 27 | /bin/ 28 | 29 | # NATS stuff 30 | gnatsd.log 31 | currentRedisConfig.json 32 | config.props 33 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | before_install: 2 | - echo "MAVEN_OPTS='-Xmx2g -XX:+TieredCompilation -XX:TieredStopAtLevel=1'" > ~/.mavenrc 3 | 4 | language: java 5 | 6 | jdk: 7 | - oraclejdk8 8 | 9 | services: 10 | - redis-server 11 | 12 | script: 13 | - mvn clean verify -B -U 14 | 15 | after_success: 16 | - mvn coveralls:report 17 | 18 | cache: 19 | directories: 20 | - "$HOME/.m2" 21 | -------------------------------------------------------------------------------- /CODE-OF-CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Community Code of Conduct 2 | 3 | NATS follows the [CNCF Code of Conduct](https://github.com/cncf/foundation/blob/master/code-of-conduct.md). 4 | -------------------------------------------------------------------------------- /GOVERNANCE.md: -------------------------------------------------------------------------------- 1 | # NATS Redis Publish Subscribe Connector Governance 2 | 3 | NATS Redis Publish Subscribe Connector (nats-connector-redis) is part of the NATS project and is subject to the [NATS Governance](https://github.com/nats-io/nats-general/blob/master/GOVERNANCE.md). -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /MAINTAINERS.md: -------------------------------------------------------------------------------- 1 | # Maintainers 2 | 3 | Maintainership is on a per project basis. 4 | 5 | ### Maintainers 6 | - Alberto Ricart [@aricart](https://github.com/aricart) 7 | - Colin Sullivan [@ColinSullivan1](https://github.com/ColinSullivan1) 8 | - Derek Collison [@derekcollison](https://github.com/derekcollison) 9 | - Ivan Kozlovic [@kozlovic](https://github.com/kozlovic) 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NATS Redis Publish Subscribe Connector 2 | A pluggable [Java](http://www.java.com) based service to bridge the [NATS messaging system](https://nats.io) and other technologies. 3 | 4 | [![License Apache 2.0](https://img.shields.io/badge/License-Apache2-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0) 5 | [![Build Status](https://travis-ci.org/nats-io/nats-connector-redis.svg?branch=master)](http://travis-ci.org/nats-io/nats-connector-redis) 6 | [![Javadoc](http://javadoc-badge.appspot.com/io.nats/nats-connector-redis.svg?label=javadoc)](http://nats-io.github.io/nats-connector-redis) 7 | [![Coverage Status](https://coveralls.io/repos/nats-io/nats-connector-redis/badge.svg?branch=master&service=github)](https://coveralls.io/github/nats-io/nats-connector-redis?branch=master) 8 | 9 | 10 | ## Summary 11 | 12 | The NATS Redis publish subscribe connector is provided to facilitate the bridging of NATS and Redis Publish/Subscribe. See [The NATS Connector Framework](https://github.com/nats-io/nats-connector-framework) for more information. 13 | 14 | Documentation can be found [here](http://nats-io.github.io/nats-connector-redis). 15 | 16 | ## Installation 17 | 18 | ### Maven Central 19 | 20 | #### Releases 21 | 22 | The NATS Redis connector is currently alpha and there are no official releases. 23 | 24 | #### Snapshots 25 | 26 | Snapshots are regularly uploaded to the Sonatype OSSRH (OSS Repository Hosting) using 27 | the same Maven coordinates. 28 | If you are embedding the NATS Redis connector, add the following dependency to your project's `pom.xml`. 29 | 30 | ```xml 31 | 32 | ... 33 | 34 | io.nats 35 | nats-connector-redis 36 | 0.1.0-SNAPSHOT 37 | 38 | 39 | ``` 40 | If you don't already have your pom.xml configured for using Maven snapshots, you'll also need to add the following repository to your pom.xml. 41 | 42 | ```xml 43 | 44 | ... 45 | 46 | sonatype-snapshots 47 | https://oss.sonatype.org/content/repositories/snapshots 48 | 49 | true 50 | 51 | 52 | 53 | ``` 54 | #### Building from source code (this repository) 55 | First, download the source code: 56 | ``` 57 | git clone git@github.com:nats-io/nats-connector-redis.git . 58 | ``` 59 | 60 | To build the library, use [maven](https://maven.apache.org/). From the root directory of the project: 61 | 62 | ``` 63 | mvn package verify 64 | ``` 65 | The jar file will be built in the `target` directory. Then copy the jar file to your classpath and you're all set. 66 | 67 | NOTE: Running the unit tests requires that `gnatsd` be installed on your system and available in your executable search path, and the redis server must be installed and running at the default port. 68 | 69 | 70 | ### Source code (this repository) 71 | To download the source code: 72 | ``` 73 | git clone git@github.com:nats-io/nats-connector-redis.git . 74 | ``` 75 | 76 | To build the library, use [maven](https://maven.apache.org/). From the root directory of the project: 77 | 78 | ``` 79 | mvn verify package 80 | ``` 81 | 82 | ## NATS Redis Connector source package structure 83 | 84 | * io.nats.connector.plugins.redis - This redis plug-in, developed by the NATS team. 85 | 86 | 87 | ### Referencing the Redis plugin from the NATS connector framework 88 | 89 | The redis plug-in referenced by the nats connector framework is: 90 | ``` 91 | com.io.nats.connector.plugins.redis.RedisPubSubPlugin 92 | ``` 93 | 94 | #### Configuration 95 | 96 | NATS configuration is set through the jnats client library properties and can be passed into the jvm, or specified in a configuration file. The properties are described [here](http://nats-io.github.io/jnats/io/nats/client/Constants.html). 97 | 98 | The NATS Redis connector is configured by specifying a url that returns JSON file as a system property. In this example, 99 | the url specifies a local file. It can be any location that meets the URI standard. 100 | 101 | ``` 102 | -Dnats.io.connector.plugins.redispubsub.configurl="file:///Users/colinsullivan/redis_nats_connector.json" 103 | ``` 104 | in code: 105 | ``` 106 | System.setProperty(RedisPubSubPlugin.CONFIG_URL, "file:///Users/colinsullivan/redis_nats_connector.json"); 107 | ``` 108 | 109 | The Redis Pub/Sub plugin configuration file read at the URI must have the following format: 110 | 111 | ``` 112 | { 113 | "host" : "localhost", 114 | "port" : 6379, 115 | "timeout" : 2000, 116 | "nats_to_redis_map" : [ 117 | { 118 | "subject" : "Export.Redis", 119 | "channel" : "Import_NATS" 120 | } 121 | ], 122 | "redis_to_nats_map" : [ 123 | { 124 | "channel" : "Export_NATS", 125 | "subject" : "Import.Redis", 126 | } 127 | ] 128 | } 129 | 130 | ``` 131 | 132 | * Host is the redis cluster host. 133 | * Port is the redis port. 134 | * Timeout is the Redis timeout. 135 | 136 | The nats_to_redis_map array is a list of NATS subjects mapped to Redis channels. NATS wildcarding is supported. 137 | So, in this case, any messages published to Export.Redis in the NATS cluster will be received, and published onto 138 | the Redis Channel "Import_NATS". 139 | 140 | From the other direction, any message published from redis on the Export_NATS channel, will be published into 141 | the NATS cluster on the Import.Redis subject. At least one map needs to be defined. 142 | 143 | Wildcarding and pattern matching is not supported at this time. 144 | 145 | Circular message routes generated by overlapping maps should be avoided. 146 | 147 | Basic circular route detection is performed, but is not considered a fatal error and plugin will operate. 148 | 149 | ## Running the NATS Redis connector 150 | 151 | There are two ways to launch the NATS Redis Pub/Sub connector - invoking the connector as an application or programatically from your own application. 152 | 153 | To invoke the connector from the command line: 154 | 155 | ``` 156 | java -classpath io.nats.connector.redis.RedisPubSubConnector 157 | ``` 158 | 159 | The arguments are optional: 160 | ``` 161 | -configURL 162 | -debug 163 | ``` 164 | 165 | To invoke the connector from an application: 166 | ``` 167 | System.setProperty(Connector.PLUGIN_CLASS, "com.io.nats.connector.plugins.redis.RedisPubSubPlugin"); 168 | new Connector().run(); 169 | ``` 170 | or finally, use the framework itself to run the redis connector: 171 | ``` 172 | java -Dio.nats.connector.plugin=com.io.nats.connector.plugins.redis.RedisPubSubPlugin io.nats.connector.Connector 173 | ``` 174 | 175 | If not using maven, ensure your classpath includes the most current nats-connector (nats-connector-0.1.5-SNAPSHOT.jar) and jnats (jnats-0.4.1.jar) archives, as well as java archives compatible with jedis-2.7.3.jar, commons-pool2-2.4.2.jar, slf4j-simple-1.7.14.jar, slf4j-api-1.7.14.jar, and json-20151123.jar. 176 | 177 | ## TODO 178 | 179 | - [ ] Wildcard Support 180 | - [ ] Auth (password) 181 | - [ ] Failover/Clustering Support 182 | 183 | ## License 184 | 185 | Unless otherwise noted, the NATS source files are distributed 186 | under the Apache Version 2.0 license found in the LICENSE file. -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | io.nats 8 | nats-parent 9 | 1.6 10 | 11 | 12 | io.nats 13 | nats-connector-redis 14 | 0.1.0-SNAPSHOT 15 | bundle 16 | 17 | nats-connector-redis 18 | Redis Plugin for NATS Connector 19 | https://github.com/nats-io/nats-connector-redis 20 | 2016 21 | 22 | 23 | https://github.com/nats-io/nats-connector-redis 24 | scm:git:git://github.com/nats-io/nats-connector-redis.git 25 | scm:git:git@github.com:nats-io/nats-connector-redis.git 26 | 27 | 28 | https://github.com/nats-io/nats-connector-redis/issues/ 29 | GitHub Issues 30 | 31 | 32 | Travis 33 | https://travis-ci.org/nats-io/nats-connector-redis/ 34 | 35 | 36 | 37 | 38 | dev 39 | ${project.build.directory}/coverage-reports/jacoco-it.exec 40 | ${project.build.directory}/coverage-reports/jacoco-ut.exec 41 | 42 | true 43 | false 44 | 45 | 46 | 1.7 47 | 1.7 48 | 49 | 50 | 0.6.0 51 | 0.1.5-SNAPSHOT 52 | 2.7.3 53 | 20140107 54 | 55 | 56 | 0.9.4 57 | gnatsd 58 | ${server.exec.name}-v${server.version}-${nats.os}-${nats.arch} 59 | https://github.com/nats-io/${server.exec.name}/releases/download/v${server.version}/${server.exec.pkg}.zip 60 | 61 | 62 | 63 | 64 | 65 | org.apache.maven.plugins 66 | maven-javadoc-plugin 67 | 68 | 69 | 70 | 71 | org.apache.maven.plugins 72 | maven-antrun-plugin 73 | 74 | 75 | 76 | 77 | prepare 78 | generate-sources 79 | 80 | 83 | 85 | 88 | 89 | 91 | 93 | 96 | 100 | 104 | 106 | 107 | 108 | 109 | run 110 | 111 | 112 | 113 | 114 | 115 | 117 | org.apache.felix 118 | maven-bundle-plugin 119 | true 120 | 121 | 122 | org.jacoco 123 | jacoco-maven-plugin 124 | 125 | 127 | 128 | pre-unit-test 129 | 130 | prepare-agent 131 | 132 | 133 | 135 | ${jacoco.ut.execution.data.file} 136 | 138 | surefireArgLine 139 | 140 | 141 | 143 | 144 | post-unit-test 145 | test 146 | 147 | report 148 | 149 | 150 | 152 | ${jacoco.ut.execution.data.file} 153 | 155 | ${project.reporting.outputDirectory}/jacoco-ut 156 | 157 | 158 | 159 | 161 | 162 | pre-integration-test 163 | pre-integration-test 164 | 165 | prepare-agent 166 | 167 | 168 | 170 | ${jacoco.it.execution.data.file} 171 | 173 | failsafeArgLine 174 | 175 | 176 | 178 | 179 | post-integration-test 180 | post-integration-test 181 | 182 | report 183 | 184 | 185 | 187 | ${jacoco.it.execution.data.file} 188 | 190 | ${project.reporting.outputDirectory}/jacoco-it 191 | 192 | 193 | 194 | 195 | 196 | 197 | org.apache.maven.plugins 198 | maven-surefire-plugin 199 | 200 | 202 | ${surefireArgLine} 203 | 205 | ${skip.unit.tests} 206 | 207 | 208 | **/IT*.java 209 | 210 | 211 | 212 | 213 | default-test 214 | test 215 | 216 | test 217 | 218 | 219 | ${skip.unit.tests} 220 | io.nats.connector.plugins.redis.UnitTest 221 | 222 | 223 | 224 | 225 | 226 | 227 | org.apache.maven.plugins 228 | maven-failsafe-plugin 229 | 230 | 232 | 233 | integration-tests 234 | 235 | integration-test 236 | verify 237 | 238 | 239 | 241 | ${failsafeArgLine} 242 | 243 | 245 | ${skip.integration.tests} 246 | io.nats.client.IntegrationTest 247 | 248 | 249 | 250 | 251 | 252 | org.eluder.coveralls 253 | coveralls-maven-plugin 254 | 255 | 256 | src/main/java 257 | examples 258 | 259 | ${jacoco.ut.execution.data.file} 260 | 261 | 262 | ${project.reporting.outputDirectory}/jacoco-ut/jacoco.xml 263 | 264 | 265 | ${jacoco.ut.execution.data.file} 266 | ${project.reporting.sourceEncoding} 267 | 268 | 273 | 274 | 275 | org.codehaus.mojo 276 | build-helper-maven-plugin 277 | 278 | 279 | generate-sources 280 | 281 | add-source 282 | 283 | 284 | 285 | examples 286 | 287 | 288 | 289 | 290 | 291 | add-test-source 292 | process-resources 293 | 294 | add-test-source 295 | 296 | 297 | 298 | src/it/java 299 | 300 | 301 | 302 | 303 | 304 | 305 | com.versioneye 306 | versioneye-maven-plugin 307 | 308 | 309 | 310 | 311 | 312 | 313 | io.nats 314 | jnats 315 | ${jnats-version} 316 | 317 | 318 | io.nats 319 | nats-connector-framework 320 | ${nats-connector-framework-version} 321 | 322 | 323 | org.slf4j 324 | slf4j-simple 325 | ${slf4j-version} 326 | 327 | 328 | redis.clients 329 | jedis 330 | ${redis-version} 331 | jar 332 | compile 333 | 334 | 335 | org.json 336 | json 337 | ${json-version} 338 | compile 339 | 340 | 341 | junit 342 | junit 343 | test 344 | 345 | 346 | org.mockito 347 | mockito-core 348 | test 349 | 350 | 351 | 352 | 353 | -------------------------------------------------------------------------------- /src/main/java/io/nats/connector/plugins/redis/RedisPubSubPlugin.java: -------------------------------------------------------------------------------- 1 | // Copyright 2016-2018 The NATS Authors 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | // 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | // 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | package io.nats.connector.plugins.redis; 15 | 16 | import io.nats.client.ConnectionFactory; 17 | import io.nats.client.Message; 18 | import io.nats.connector.plugin.NATSConnector; 19 | import io.nats.connector.plugin.NATSConnectorPlugin; 20 | import io.nats.connector.plugin.NATSEvent; 21 | import io.nats.connector.plugin.NATSUtilities; 22 | import org.json.*; 23 | import org.slf4j.Logger; 24 | 25 | import redis.clients.jedis.*; 26 | import redis.clients.jedis.exceptions.*; 27 | 28 | import java.util.ArrayList; 29 | import java.util.Properties; 30 | 31 | import java.util.concurrent.ExecutorService; 32 | import java.util.concurrent.Executors; 33 | 34 | import java.util.HashMap; 35 | import java.util.Map.Entry; 36 | import java.util.List; 37 | 38 | /** 39 | * A Redis publish/subscribe connector plugin. 40 | *

41 | * It reads a configuration file from a provided url to direct 42 | * the connector to bridge NATS and Redis. 43 | *

44 | * The file is JSON formatted with the following structure: 45 | *

 46 |  * {
 47 |  *   "host" : "localhost",
 48 |  *   "port" : 6379,
 49 |  *   "timeout" : 2000,
 50 |  *   "nats_to_redis_map" : [
 51 |  *     {
 52 |  *       "subject" : "Export.Redis",
 53 |  *       "channel" : "Import_NATS"
 54 |  *     }
 55 |  *   ],
 56 |  *   "redis_to_nats_map" : [
 57 |  *     {
 58 |  *       "channel" : "Export_NATS",
 59 |  *       "subject" : "Import.Redis",
 60 |  *     }
 61 |  *   ]
 62 |  * }
 63 |  * 
64 | *

65 | * NATs publishing to Redis.Export will send messages to Redis on channel 66 | * NATS.import. 67 | *

68 | * Redis publishing to NATS.Export will send messages to NATS on subject 69 | * Redis.Import. 70 | *

71 | * This is highly customizable by adding multiple subscriptions. 72 | *

73 | * Wildcards/Patters are not yet supported. 74 | *

75 | * Take care to avoid circular routes generated 76 | * by overlapping maps should be avoided. 77 | */ 78 | public class RedisPubSubPlugin implements NATSConnectorPlugin { 79 | 80 | /** 81 | * The property location to specify a configuration URL. 82 | */ 83 | static public final String CONFIG_URL = "nats.io.connector.plugins.redispubsub.configurl"; 84 | 85 | /** 86 | * Default redis host. 87 | */ 88 | static public final String DEFAULT_REDIS_HOST = "localhost"; 89 | 90 | /** 91 | * Default redis port. 92 | */ 93 | static public final int DEFAULT_REDIS_PORT = 6379; 94 | 95 | /** 96 | * Default redis timeout. 97 | */ 98 | static public final int DEFAULT_REDIS_TIMEOUT = 2000; 99 | 100 | NATSConnector connector = null; 101 | Logger logger = null; 102 | 103 | boolean trace = false; 104 | 105 | private HashMap> channelsToSubjects = null; 106 | private HashMap> subjectsToChannels = null; 107 | 108 | JedisPool jedisPool = null; 109 | BinaryJedis publishJedis = null; 110 | 111 | Object redisPublishLock = new Object(); 112 | 113 | ListenForRedisUpdates listener = null; 114 | 115 | String host = DEFAULT_REDIS_HOST; 116 | int port = DEFAULT_REDIS_PORT; 117 | int timeout = DEFAULT_REDIS_TIMEOUT; 118 | 119 | String configUrl = null; 120 | 121 | String defaultConfiguration = 122 | "{" + 123 | "\"host\" : \"" + DEFAULT_REDIS_HOST + "\"," + 124 | "\"port\" : \""+ DEFAULT_REDIS_PORT + "\"," + 125 | "\"timeout\" : \"" + DEFAULT_REDIS_TIMEOUT + "\"," + 126 | "\"nats_to_redis_map\" : [" + 127 | "{" + 128 | "\"subject\" : \"Export.Redis\"," + 129 | "\"channel\" : \"Import_NATS\"" + 130 | "}" + 131 | "]," + 132 | "\"redis_to_nats_map\" : [" + 133 | "{" + 134 | "\"channel\" : \"Export_NATS\"," + 135 | "\"subject\" : \"Import.Redis\"" + 136 | "}" + 137 | "]" + 138 | "}"; 139 | 140 | 141 | private void loadProperties() 142 | { 143 | Properties p = System.getProperties(); 144 | configUrl = p.getProperty(CONFIG_URL); 145 | } 146 | 147 | private void checkMapObj(JSONObject jo) throws Exception 148 | { 149 | if (jo.has("channel") == false) 150 | throw new Exception("channel not defined in map."); 151 | 152 | if (jo.has("subject") == false) 153 | throw new Exception("subject not defined in map."); 154 | } 155 | 156 | private boolean isNatsSubjectValid(String subject) 157 | { 158 | if (subject == null) 159 | { 160 | logger.warn("Null NATS subjects are not valid."); 161 | return false; 162 | } 163 | 164 | if (subject.isEmpty()) 165 | { 166 | logger.warn("Empty NATS subject is are not valid."); 167 | return false; 168 | } 169 | 170 | if (subject.contains(">") || subject.contains("*")) 171 | { 172 | logger.warn("Wildcard NATS subject {} is not supported.", subject); 173 | return false; 174 | } 175 | 176 | return true; 177 | } 178 | 179 | private boolean listContains(List list, String value) 180 | { 181 | for (String s : list) 182 | { 183 | if (s.equals(value)) 184 | { 185 | // just silently ignore duplicate mappings. 186 | return true; 187 | } 188 | } 189 | 190 | return false; 191 | } 192 | 193 | private void addToMap(HashMap> map, String key, String value) 194 | { 195 | List l = null; 196 | 197 | if (map == null) 198 | return; 199 | 200 | if (map.containsKey(key) == false) 201 | { 202 | l = new ArrayList(); 203 | l.add(value); 204 | map.put(key, l); 205 | } 206 | else 207 | { 208 | l = map.get(key); 209 | 210 | // just silenly ignore duplicates. 211 | if (listContains(l, value)) 212 | return; 213 | 214 | l.add(value); 215 | } 216 | } 217 | 218 | private void parseRedisToNatsMapObj(JSONObject destMapObj) throws Exception 219 | { 220 | checkMapObj(destMapObj); 221 | 222 | if (channelsToSubjects == null) 223 | channelsToSubjects = new HashMap>(); 224 | 225 | String channel = destMapObj.getString("channel"); 226 | String subject = destMapObj.getString("subject"); 227 | 228 | if (!isNatsSubjectValid(subject)) 229 | throw new Exception("Invalid subject: " + subject); 230 | 231 | logger.debug("Mapping Redis channel {} to NATS subject {}", 232 | channel, subject); 233 | 234 | addToMap(channelsToSubjects, channel, subject); 235 | } 236 | 237 | private void parseNatsToRedisMapObj(JSONObject destMapObj) throws Exception 238 | { 239 | checkMapObj(destMapObj); 240 | 241 | if (subjectsToChannels == null) 242 | subjectsToChannels = new HashMap>(); 243 | 244 | String channel = destMapObj.getString("channel"); 245 | String subject = destMapObj.getString("subject"); 246 | 247 | if (!isNatsSubjectValid(subject)) 248 | throw new Exception("Invalid subject: " + subject); 249 | 250 | logger.debug("Mapping NATS subject {} to Redis channel {}", 251 | subject, channel); 252 | 253 | addToMap(subjectsToChannels, subject, channel); 254 | } 255 | 256 | // Warn against circular routes; they don't end well. 257 | // 258 | // TODO: Wildcard/Pattern checks someday. 259 | private void warnOnCircularRoute() 260 | { 261 | if (subjectsToChannels == null || channelsToSubjects == null) 262 | return; 263 | 264 | // find NATS subject to redis subject matches 265 | for (Entry> subjEntry : subjectsToChannels.entrySet()) 266 | { 267 | for (Entry> chanEntry : channelsToSubjects.entrySet()) 268 | { 269 | if (listContains(chanEntry.getValue(), subjEntry.getKey()) && 270 | listContains(subjEntry.getValue(), (chanEntry.getKey()))) 271 | { 272 | logger.error("Circular route found between subject '{}' and channel '{}'", 273 | subjEntry.getKey(), subjEntry.getValue()); 274 | } 275 | } 276 | } 277 | } 278 | 279 | 280 | /** 281 | * Gets the default configuration. 282 | * @return default configuration as a JSON string. 283 | */ 284 | String getDefaultConfiguration() 285 | { 286 | return defaultConfiguration; 287 | } 288 | 289 | private void loadConfig() throws Exception { 290 | 291 | String configStr = getDefaultConfiguration(); 292 | 293 | if (configUrl != null) { 294 | configStr = NATSUtilities.readFromUrl(configUrl); 295 | } 296 | 297 | parseConfiguration(configStr); 298 | } 299 | 300 | /** 301 | * Public for testing purposes. 302 | * 303 | * @param jsonConfig - json configuration in a string. 304 | * @throws Exception - an error occurred parsing the configuration. 305 | */ 306 | public void parseConfiguration(String jsonConfig) throws Exception 307 | { 308 | JSONArray ja; 309 | 310 | JSONObject rootConfig = new JSONObject(new JSONTokener(jsonConfig)); 311 | 312 | host = rootConfig.optString("host", DEFAULT_REDIS_HOST); 313 | port = rootConfig.optInt("port", DEFAULT_REDIS_PORT); 314 | timeout = rootConfig.optInt("timeout", DEFAULT_REDIS_TIMEOUT); 315 | 316 | if (rootConfig.has("nats_to_redis_map")) { 317 | ja = rootConfig.getJSONArray("nats_to_redis_map"); 318 | if (ja != null) { 319 | for (int i = 0; i < ja.length(); i++) { 320 | parseNatsToRedisMapObj((JSONObject) ja.get(i)); 321 | } 322 | } 323 | } 324 | 325 | if (rootConfig.has("redis_to_nats_map")) { 326 | ja = rootConfig.getJSONArray("redis_to_nats_map"); 327 | if (ja != null) { 328 | for (int i = 0; i < ja.length(); i++) { 329 | parseRedisToNatsMapObj((JSONObject) ja.get(i)); 330 | } 331 | } 332 | } 333 | 334 | warnOnCircularRoute(); 335 | } 336 | 337 | private List getChannelsFromSubject(String subject) 338 | { 339 | if (subjectsToChannels == null) 340 | return null; 341 | 342 | return subjectsToChannels.get(subject); 343 | } 344 | 345 | private List getSubjectsFromChannel(String channel) 346 | { 347 | if (channelsToSubjects == null) 348 | return null; 349 | 350 | return channelsToSubjects.get(channel); 351 | } 352 | 353 | private class JedisListener extends JedisPubSub 354 | { 355 | List l; 356 | 357 | Message natsMessage = new Message(); 358 | 359 | private void sendNatsMessage(String channelOrPattern, String message) 360 | { 361 | l = getSubjectsFromChannel(channelOrPattern); 362 | 363 | byte[] payload = message.getBytes(); 364 | natsMessage.setData(payload, 0, payload.length); 365 | 366 | for (String s : l) { 367 | natsMessage.setSubject(s); 368 | connector.publish(natsMessage); 369 | 370 | logger.trace("Send Redis ({}) -> NATS ({})", channelOrPattern, s); 371 | } 372 | } 373 | 374 | @Override 375 | public void onMessage(String channel, String message) 376 | { 377 | sendNatsMessage(channel, message); 378 | super.onMessage(channel, message); 379 | } 380 | 381 | @Override 382 | public void onSubscribe(String channel, int subscribedChannels) 383 | { 384 | logger.debug("Subscribed to Redis channel {} ({})", channel, subscribedChannels); 385 | 386 | super.onSubscribe(channel, subscribedChannels); 387 | } 388 | 389 | @Override 390 | public void onUnsubscribe(String channel, int subscribedChannels) 391 | { 392 | logger.debug("Unsubscribed to Redis channel {} ({})", channel, subscribedChannels); 393 | 394 | super.onUnsubscribe(channel, subscribedChannels); 395 | } 396 | 397 | @Override 398 | public void onPSubscribe(String pattern, int subscribedChannels) 399 | { 400 | logger.debug("Subscribed to Redis pattern {} ({})", pattern, subscribedChannels); 401 | 402 | super.onPSubscribe(pattern, subscribedChannels); 403 | } 404 | 405 | @Override 406 | public void onPUnsubscribe(String pattern, int subscribedChannels) 407 | { 408 | logger.debug("Unsubscribed from Redis pattern {} ({})", pattern, subscribedChannels); 409 | 410 | super.onPUnsubscribe(pattern, subscribedChannels); 411 | } 412 | 413 | @Override 414 | public void onPMessage(String pattern, String channel, 415 | String message) 416 | { 417 | sendNatsMessage(channel, message); 418 | 419 | super.onPMessage(pattern, channel, message); 420 | } 421 | } 422 | 423 | private class ListenForRedisUpdates implements Runnable 424 | { 425 | private boolean running = true; 426 | private Object runningLock = new Object(); 427 | Jedis listenJedis = null; 428 | 429 | private void setRunning(boolean value) 430 | { 431 | synchronized (runningLock) 432 | { 433 | running = value; 434 | } 435 | } 436 | 437 | private boolean isRunning() 438 | { 439 | synchronized (runningLock) 440 | { 441 | return running; 442 | } 443 | } 444 | 445 | private String[] buildJedisSubscribeChannels() 446 | { 447 | if (channelsToSubjects == null) 448 | return null; 449 | 450 | if (channelsToSubjects.size() < 1) 451 | return null; 452 | 453 | return channelsToSubjects.keySet().toArray(new String[0]); 454 | } 455 | 456 | private void logChannels(String[] channels) 457 | { 458 | if (logger.isDebugEnabled() == false) 459 | return; 460 | 461 | if (channels == null || channels.length == 0) { 462 | logger.debug("Not subscribing to any redis channels."); 463 | } 464 | 465 | for (String chan : channels) { 466 | logger.debug("Subscribing to redis channel: {}", chan); 467 | } 468 | 469 | } 470 | 471 | @Override 472 | public void run() 473 | { 474 | listenJedis = jedisPool.getResource(); 475 | 476 | while (isRunning()) 477 | { 478 | try { 479 | String[] channels = buildJedisSubscribeChannels(); 480 | logChannels(channels); 481 | if (channels == null) 482 | { 483 | shutdown(); 484 | break; 485 | } 486 | listenJedis.subscribe(new JedisListener(), channels); 487 | } 488 | catch (JedisConnectionException e) 489 | { 490 | logger.error("Lost connection to the Redis server. Exiting."); 491 | 492 | // TODO: retry logic? 493 | shutdown(); 494 | } 495 | } 496 | 497 | listenJedis.close(); 498 | } 499 | 500 | public void shutdown() 501 | { 502 | setRunning(false); 503 | listenJedis.close(); 504 | } 505 | } 506 | 507 | public RedisPubSubPlugin() {} 508 | 509 | private void initJedis() 510 | { 511 | logger.debug("Initializing Redis."); 512 | 513 | jedisPool = new JedisPool(new JedisPoolConfig(), host, port, timeout); 514 | publishJedis = jedisPool.getResource(); 515 | } 516 | 517 | private void teardownJedis() 518 | { 519 | logger.debug("Cleaning up Redis Resources."); 520 | 521 | if (listener != null) 522 | listener.shutdown(); 523 | 524 | if (publishJedis != null) 525 | publishJedis.close(); 526 | 527 | if (jedisPool != null) 528 | jedisPool.close(); 529 | } 530 | 531 | @Override 532 | public boolean onStartup(Logger logger, ConnectionFactory factory) { 533 | this.logger = logger; 534 | 535 | try { 536 | loadProperties(); 537 | loadConfig(); 538 | initJedis(); 539 | } 540 | catch (Exception e) { 541 | logger.error("Unable to initialize.", e); 542 | teardownJedis(); 543 | return false; 544 | } 545 | 546 | return true; 547 | } 548 | 549 | @Override 550 | public boolean onNatsInitialized(NATSConnector connector) 551 | { 552 | this.connector = connector; 553 | 554 | if (subjectsToChannels == null && channelsToSubjects == null) 555 | { 556 | logger.error("No subject/channel mapping has been defined."); 557 | return false; 558 | } 559 | 560 | try { 561 | if (subjectsToChannels != null) { 562 | for (String s : subjectsToChannels.keySet()) { 563 | connector.subscribe(s); 564 | } 565 | } 566 | } 567 | catch (Exception e) 568 | { 569 | logger.error("NATS Subscription error", e); 570 | return false; 571 | } 572 | 573 | if (channelsToSubjects != null) { 574 | ExecutorService executor = Executors.newSingleThreadExecutor(); 575 | executor.execute(new ListenForRedisUpdates()); 576 | } 577 | 578 | return true; 579 | } 580 | 581 | @Override 582 | public void onShutdown() 583 | { 584 | teardownJedis(); 585 | } 586 | 587 | @Override 588 | public void onNATSMessage(Message msg) 589 | { 590 | String subject = msg.getSubject(); 591 | List channels = getChannelsFromSubject(subject); 592 | if (channels == null) 593 | { 594 | logger.error("Cannot publish from NATS to Redis - unmapped subject '" + subject + "'"); 595 | return; 596 | } 597 | 598 | byte[] payload = msg.getData(); 599 | 600 | for (String s : channels) 601 | { 602 | 603 | byte[] channel = s.getBytes(); 604 | 605 | // NOTE: in tests, one can get an EOS exception here when shutting down. 606 | // 607 | // Also, a jedis instance is not threadsafe. Right now simply lock. 608 | // If we need better performance, investigate using one per 609 | // NATS subject. 610 | synchronized (redisPublishLock) { 611 | publishJedis.publish(channel, payload); 612 | } 613 | 614 | logger.trace("Send NATS ({}) -> Redis ({})", subject, new String(channel)); 615 | } 616 | } 617 | 618 | @Override 619 | public void onNATSEvent(NATSEvent event, String message) 620 | { 621 | // When a connection has been disconnected unexpectedly, NATS will 622 | // try to reconnect. Messages published during the reconnect will 623 | // be buffered and resent, so there may be no need to do anything. 624 | // Connection disconnected - close JEDIS, buffer messages? 625 | // Reconnected - reconnect to JEDIS. 626 | // Closed: should handle elsewhere. 627 | // Async error. Notify, let admins handle these. 628 | switch (event) 629 | { 630 | case ASYNC_ERROR: 631 | logger.error("NATS Asynchronous error: " + message); 632 | break; 633 | case RECONNECTED: 634 | logger.info("Reconnected to the NATS cluster: " + message); 635 | // At this point, we may not have to do much. Buffered NATS messages 636 | // may be flushed. and we'll buffer and flush the Redis messages. 637 | // Revisit this later if we need more buffering. 638 | break; 639 | case DISCONNECTED: 640 | logger.info("Disconnected from the NATS cluster: " + message); 641 | break; 642 | case CLOSED: 643 | logger.debug("NATS Event Connection Closed: " + message); 644 | // shudown - if this is a result of shutdown elsewhere, 645 | // there will be no effect. 646 | connector.shutdown(); 647 | break; 648 | default: 649 | logger.warn("Unknown NATS Event: " + message); 650 | } 651 | } 652 | } 653 | -------------------------------------------------------------------------------- /src/main/java/io/nats/connector/redis/RedisPubSubConnector.java: -------------------------------------------------------------------------------- 1 | // Copyright 2016-2018 The NATS Authors 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | // 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | // 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | package io.nats.connector.redis; 14 | 15 | import io.nats.connector.Connector; 16 | import io.nats.connector.plugins.redis.RedisPubSubPlugin; 17 | 18 | /** 19 | * A Utility class to launch the Redis Connector from the command line. 20 | */ 21 | public class RedisPubSubConnector { 22 | 23 | /** 24 | * Usage. 25 | */ 26 | static void usage() { 27 | System.out.printf("java %s\n", RedisPubSubConnector.class.getCanonicalName()); 28 | System.out.printf(" -configURL \n" + 29 | " -debug\n"); 30 | System.exit(-1); 31 | } 32 | 33 | static void parseArgs(String[] args) 34 | { 35 | if(args == null) 36 | return; 37 | 38 | if (args.length == 0) 39 | return; 40 | 41 | if(args.length < 2) { 42 | RedisPubSubConnector.usage(); 43 | } 44 | 45 | for (int i = 0; i < args.length; i++) 46 | { 47 | if("-configURL".equalsIgnoreCase(args[i])) 48 | { 49 | i++; 50 | if (i >= args.length) { 51 | usage(); 52 | } 53 | System.setProperty(RedisPubSubPlugin.CONFIG_URL, args[i]); 54 | } 55 | else if ("-debug".equalsIgnoreCase(args[i])) 56 | { 57 | System.setProperty("org.slf4j.simpleLogger.log.io.nats.connector.plugins.redis.RedisPubSubPlugin", "trace"); 58 | } 59 | else 60 | { 61 | RedisPubSubConnector.usage(); 62 | } 63 | } 64 | } 65 | 66 | /*** 67 | * Entry point to launch the NATS Redis Connector 68 | * @param args - arguments. See usage for more information. 69 | */ 70 | public static void main(String[] args) 71 | { 72 | try 73 | { 74 | parseArgs(args); 75 | 76 | System.setProperty(Connector.PLUGIN_CLASS, RedisPubSubPlugin.class.getName()); 77 | new Connector(null).run(); 78 | } 79 | catch (Exception e) 80 | { 81 | System.err.println(e); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/test/java/io/nats/connector/plugins/redis/NATSServer.java: -------------------------------------------------------------------------------- 1 | // Copyright 2016-2018 The NATS Authors 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | // 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | // 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | package io.nats.connector.plugins.redis; 15 | 16 | import java.io.File; 17 | import java.io.IOException; 18 | import java.nio.file.Paths; 19 | import java.util.ArrayList; 20 | import java.util.Arrays; 21 | import java.util.List; 22 | 23 | class NATSServer implements Runnable, AutoCloseable { 24 | static final String GNATSD = "gnatsd"; 25 | static final String TEST_RESOURCE_DIR = "src/test/resources"; 26 | 27 | // Enable this for additional server debugging info. 28 | boolean debug = false; 29 | 30 | ProcessBuilder pb; 31 | Process p; 32 | ProcessStartInfo psInfo; 33 | 34 | class ProcessStartInfo { 35 | List arguments = new ArrayList(); 36 | 37 | public ProcessStartInfo(String command) { 38 | this.arguments.add(command); 39 | } 40 | 41 | public void addArgument(String arg) { 42 | this.arguments.addAll(Arrays.asList(arg.split("\\s+"))); 43 | } 44 | 45 | String[] getArgsAsArray() { 46 | return arguments.toArray(new String[arguments.size()]); 47 | } 48 | 49 | String getArgsAsString() { 50 | String stringVal = new String(); 51 | for (String str : arguments) { 52 | stringVal = stringVal.concat(str + " "); 53 | } 54 | return stringVal.trim(); 55 | } 56 | 57 | public String toString() { 58 | return getArgsAsString(); 59 | } 60 | } 61 | 62 | public NATSServer(boolean debug) { 63 | this(-1, debug); 64 | } 65 | 66 | public NATSServer() { 67 | this(-1, false); 68 | } 69 | 70 | public NATSServer(int port) { 71 | this(port, false); 72 | } 73 | 74 | public NATSServer(int port, boolean debug) { 75 | this.debug = debug; 76 | psInfo = this.createProcessStartInfo(); 77 | 78 | if (port > 1023) { 79 | psInfo.addArgument("-p " + String.valueOf(port)); 80 | } 81 | // psInfo.addArgument("-m 8222"); 82 | 83 | start(); 84 | } 85 | 86 | private String buildConfigFileName(String configFile) { 87 | return Paths.get(TEST_RESOURCE_DIR, configFile).toAbsolutePath().toString(); 88 | } 89 | 90 | public NATSServer(String configFile, boolean debug) { 91 | this.debug = debug; 92 | psInfo = this.createProcessStartInfo(); 93 | psInfo.addArgument("-config " + buildConfigFileName(configFile)); 94 | start(); 95 | } 96 | 97 | private ProcessStartInfo createProcessStartInfo() { 98 | String path = Paths.get("target", "/", GNATSD).toAbsolutePath().toString(); 99 | psInfo = new ProcessStartInfo(path); 100 | 101 | if (debug) { 102 | psInfo.addArgument("-DV"); 103 | } 104 | 105 | return psInfo; 106 | } 107 | 108 | public void start() { 109 | try { 110 | pb = new ProcessBuilder(psInfo.arguments); 111 | pb.directory(new File("target")); 112 | if (debug) { 113 | System.err.println("Inheriting IO, psInfo =" + psInfo); 114 | pb.inheritIO(); 115 | } else { 116 | pb.redirectError(new File("/dev/null")); 117 | pb.redirectOutput(new File("/dev/null")); 118 | } 119 | p = pb.start(); 120 | if (debug) { 121 | System.out.println("Started [" + psInfo + "]"); 122 | } 123 | } catch (IOException e) { 124 | e.printStackTrace(); 125 | } 126 | } 127 | 128 | public void shutdown() { 129 | if (p == null) { 130 | return; 131 | } 132 | 133 | p.destroy(); 134 | if (debug) { 135 | System.out.println("Stopped [" + psInfo + "]"); 136 | } 137 | 138 | p = null; 139 | } 140 | 141 | @Override 142 | public void run() { 143 | // TODO Auto-generated method stub 144 | 145 | } 146 | 147 | 148 | @Override 149 | public void close() { 150 | this.shutdown(); 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /src/test/java/io/nats/connector/plugins/redis/RedisPubSubPluginTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2016-2018 The NATS Authors 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | // 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | // 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | package io.nats.connector.plugins.redis; 15 | 16 | import io.nats.connector.Connector; 17 | import io.nats.connector.plugins.redis.UnitTestUtilities; 18 | import io.nats.connector.plugins.redis.TestCasePrinterRule; 19 | import org.junit.*; 20 | import io.nats.client.*; 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | import redis.clients.jedis.*; 24 | 25 | import java.io.BufferedWriter; 26 | import java.io.File; 27 | import java.io.FileWriter; 28 | import java.util.concurrent.ExecutorService; 29 | import java.util.concurrent.Executors; 30 | import org.junit.experimental.categories.Category; 31 | 32 | 33 | @Category(UnitTest.class) 34 | public class RedisPubSubPluginTest { 35 | 36 | static final String REDIS_PAYLOAD = "Hello from Redis!"; 37 | static final String NATS_PAYLOAD = "Hello from NATS!"; 38 | 39 | Logger logger = null; 40 | 41 | @Rule 42 | public TestCasePrinterRule pr = new TestCasePrinterRule(System.out); 43 | 44 | @BeforeClass 45 | public static void setUpBeforeClass() throws Exception { 46 | UnitTestUtilities.startDefaultServer(); 47 | Thread.sleep(500); 48 | } 49 | 50 | @AfterClass 51 | public static void tearDownAfterClass() throws Exception { 52 | UnitTestUtilities.stopDefaultServer(); 53 | Thread.sleep(500); 54 | } 55 | 56 | abstract class TestClient 57 | { 58 | Object readyLock = new Object(); 59 | boolean isReady = false; 60 | 61 | String id = ""; 62 | 63 | Object completeLock = new Object(); 64 | boolean isComplete = false; 65 | 66 | protected int testCount = 0; 67 | 68 | int msgCount = 0; 69 | 70 | int tallyMessage() 71 | { 72 | return (++msgCount); 73 | } 74 | 75 | int getMessageCount() 76 | { 77 | return msgCount; 78 | } 79 | 80 | TestClient(String id, int testCount) 81 | { 82 | this.id = id; 83 | this.testCount = testCount; 84 | } 85 | 86 | void setReady() 87 | { 88 | logger.debug("Client ({}) is ready.", id); 89 | synchronized (readyLock) 90 | { 91 | if (isReady) 92 | return; 93 | 94 | isReady = true; 95 | readyLock.notifyAll(); 96 | } 97 | } 98 | 99 | void waitUntilReady() 100 | { 101 | synchronized (readyLock) 102 | { 103 | while (!isReady) { 104 | try { 105 | readyLock.wait(); 106 | } catch (InterruptedException e) { 107 | e.printStackTrace(); 108 | } 109 | } 110 | } 111 | logger.debug("Done waiting for Client ({}) to be ready.", id); 112 | } 113 | 114 | void setComplete() 115 | { 116 | logger.debug("Client ({}) has completed.", id); 117 | 118 | synchronized(completeLock) 119 | { 120 | if (isComplete) 121 | return; 122 | 123 | isComplete = true; 124 | completeLock.notifyAll(); 125 | } 126 | } 127 | 128 | void waitForCompletion() 129 | { 130 | synchronized (completeLock) 131 | { 132 | while (!isComplete) 133 | { 134 | try { 135 | completeLock.wait(); 136 | } catch (InterruptedException e) { 137 | e.printStackTrace(); 138 | } 139 | } 140 | } 141 | logger.debug("Done waiting for Client ({}) to complete.", id); 142 | } 143 | } 144 | 145 | /** 146 | * Simulates a simple Redis publisher. 147 | */ 148 | public class RedisPublisher extends TestClient implements Runnable 149 | { 150 | String channel; 151 | JedisPool jedisPool = new JedisPool(); 152 | 153 | RedisPublisher(String id, String channel, int count) 154 | { 155 | super(id, count); 156 | logger.debug("Creating Redis Publisher ({})", id); 157 | this.channel = channel; 158 | } 159 | 160 | @Override 161 | public void run() { 162 | 163 | logger.debug("RedisPublisher: {} Starting.", id); 164 | Jedis jedis = jedisPool.getResource(); 165 | jedis.connect(); 166 | 167 | logger.debug("RedisPublisher: {} connected.", id); 168 | 169 | for (int i = 0; i < testCount; i++) { 170 | jedis.publish(channel, REDIS_PAYLOAD); 171 | tallyMessage(); 172 | } 173 | 174 | logger.debug("Redis Publisher ({}) : Published {} messages", id, testCount); 175 | 176 | jedis.disconnect(); 177 | 178 | setComplete(); 179 | } 180 | } 181 | 182 | /** 183 | * Simulates a simple Redis subscriber. 184 | */ 185 | public class RedisSubscriber extends TestClient implements Runnable 186 | { 187 | boolean checkPayload = true; 188 | 189 | JedisPool jedisPool = new JedisPool(); 190 | Jedis jedis = null; 191 | String channel = null; 192 | 193 | RedisSubscriber(String id, String channel, int count) 194 | { 195 | super(id, count); 196 | logger.debug("Creating Redis Subscriber ({})", id); 197 | this.channel = channel; 198 | } 199 | 200 | @Override 201 | void waitForCompletion() { 202 | super.waitForCompletion(); 203 | jedis.disconnect(); 204 | } 205 | 206 | class RedisListener extends JedisPubSub { 207 | @Override 208 | public void onMessage(String channel, String message) { 209 | logger.trace("Redis Subscriber ({}): Received message: {}", id, message); 210 | 211 | if (checkPayload) { 212 | Assert.assertTrue(NATS_PAYLOAD.equals(message)); 213 | } 214 | 215 | if (tallyMessage() == testCount) { 216 | logger.debug("Redis Subscriber ({}): Received {} messages. Completed.", id, testCount); 217 | this.unsubscribe(); 218 | } 219 | } 220 | 221 | @Override 222 | public void onSubscribe(String channel, int subscribedChannels) { 223 | setReady(); 224 | super.onSubscribe(channel, subscribedChannels); 225 | } 226 | 227 | @Override 228 | public void onPSubscribe(String pattern, int subscribedChannels) { 229 | setReady(); 230 | super.onPSubscribe(pattern, subscribedChannels); 231 | } 232 | 233 | } 234 | 235 | @Override 236 | public void run() { 237 | 238 | RedisListener rl = new RedisListener(); 239 | 240 | jedis = jedisPool.getResource(); 241 | jedis.connect(); 242 | 243 | logger.trace("Redis Subscriber ({}): subscribing to {}.", id, channel); 244 | 245 | jedis.subscribe(rl, channel); 246 | 247 | logger.debug("Redis Subscriber ({}): Exiting.", id); 248 | 249 | setComplete(); 250 | } 251 | } 252 | 253 | /** 254 | * Simulates a simple NATS publisher. 255 | */ 256 | class NatsPublisher extends TestClient implements Runnable 257 | { 258 | String subject = null; 259 | 260 | NatsPublisher(String id, String subject, int count) 261 | { 262 | super(id, count); 263 | this.subject = subject; 264 | 265 | logger.debug("Creating NATS Publisher ({})", id); 266 | } 267 | 268 | @Override 269 | public void run() { 270 | 271 | try { 272 | 273 | logger.debug("NATS Publisher ({}): Starting", id); 274 | 275 | io.nats.client.Connection c = new ConnectionFactory().createConnection(); 276 | 277 | for (int i = 0; i < testCount; i++) { 278 | c.publish(subject, NATS_PAYLOAD.getBytes()); 279 | tallyMessage(); 280 | } 281 | c.flush(); 282 | 283 | logger.debug("NATS Publisher ({}): Published {} messages.", id, testCount); 284 | 285 | setComplete(); 286 | } 287 | catch (Exception ex) 288 | { 289 | ex.printStackTrace(); 290 | } 291 | } 292 | } 293 | 294 | /** 295 | * Simulates a simple NATS subscriber. 296 | */ 297 | class NatsSubscriber extends TestClient implements Runnable, MessageHandler 298 | { 299 | String subject = null; 300 | boolean checkPayload = true; 301 | 302 | NatsSubscriber(String id, String subject, int count) 303 | { 304 | super(id, count); 305 | this.subject = subject; 306 | 307 | logger.debug("Creating NATS Subscriber ({})", id); 308 | } 309 | 310 | @Override 311 | public void run() { 312 | 313 | try { 314 | logger.trace("NATS Subscriber ({}): Subscribing to subject: {}", id, subject); 315 | 316 | io.nats.client.Connection c = new ConnectionFactory().createConnection(); 317 | 318 | AsyncSubscription s = c.subscribeAsync(subject, this); 319 | s.start(); 320 | 321 | setReady(); 322 | 323 | logger.debug("NATS Subscriber ({}): Subscribing to subject: {}", id, subject); 324 | 325 | waitForCompletion(); 326 | 327 | s.unsubscribe(); 328 | 329 | logger.debug("NATS Subscriber ({}): Exiting.", id); 330 | } 331 | catch (Exception ex) 332 | { 333 | ex.printStackTrace(); 334 | } 335 | } 336 | 337 | @Override 338 | public void onMessage(Message message) { 339 | 340 | String value = new String (message.getData()); 341 | 342 | logger.trace("NATS Subscriber ({}): Received message: {}", id, value); 343 | 344 | if (checkPayload) { 345 | org.junit.Assert.assertTrue(REDIS_PAYLOAD.equals(value)); 346 | } 347 | 348 | if (tallyMessage() == testCount) 349 | { 350 | logger.debug("NATS Subscriber ({}) Received {} messages. Completed.", id, testCount); 351 | setComplete(); 352 | } 353 | } 354 | } 355 | 356 | private String generateContentFile(String content) throws Exception 357 | { 358 | File workingFile = new File("currentRedisConfig.json"); 359 | BufferedWriter bw = new BufferedWriter(new FileWriter(workingFile)); 360 | bw.write(content); 361 | bw.close(); 362 | 363 | return workingFile.toURI().toString(); 364 | } 365 | 366 | @Before 367 | public void initialize() 368 | { 369 | System.setProperty(Connector.PLUGIN_CLASS, RedisPubSubPlugin.class.getName()); 370 | 371 | // Enable tracing for debugging as necessary. 372 | //System.setProperty("org.slf4j.simpleLogger.log.io.nats.connector.plugins.redis.RedisPubSubPlugin", "trace"); 373 | //System.setProperty("org.slf4j.simpleLogger.log.io.nats.connector.plugins.redis.RedisPubSubPluginTest", "trace"); 374 | //System.setProperty("org.slf4j.simpleLogger.log.io.nats.client", "trace"); 375 | 376 | logger = LoggerFactory.getLogger(RedisPubSubPluginTest.class); 377 | } 378 | 379 | @Test 380 | public void testNatsToRedis() throws Exception { 381 | 382 | System.clearProperty(RedisPubSubPlugin.CONFIG_URL); 383 | 384 | Connector c = new Connector(); 385 | 386 | ExecutorService executor = Executors.newFixedThreadPool(6); 387 | 388 | RedisSubscriber rs = new RedisSubscriber("rs", "Import_NATS", 5); 389 | NatsPublisher np = new NatsPublisher("np", "Export.Redis", 5); 390 | 391 | // start the connector 392 | executor.execute(c); 393 | 394 | // start the subsciber 395 | executor.execute(rs); 396 | 397 | // wait for subscriber to be ready. 398 | rs.waitUntilReady(); 399 | 400 | // let the connector start 401 | Thread.sleep(2000); 402 | 403 | // start the publisher 404 | executor.execute(np); 405 | 406 | // wait for the subscribers to complete. 407 | rs.waitForCompletion(); 408 | 409 | Assert.assertTrue("Invalid count", rs.getMessageCount() == 5); 410 | 411 | c.shutdown(); 412 | } 413 | 414 | @Test 415 | public void testRedisToNats() throws Exception { 416 | 417 | System.clearProperty(RedisPubSubPlugin.CONFIG_URL); 418 | 419 | Connector c = new Connector(); 420 | 421 | ExecutorService executor = Executors.newFixedThreadPool(6); 422 | 423 | RedisPublisher rp = new RedisPublisher("rp", "Export_NATS", 5); 424 | NatsSubscriber ns = new NatsSubscriber("ns", "Import.Redis", 5); 425 | 426 | // start the connector 427 | executor.execute(c); 428 | 429 | // start the subsciber app 430 | executor.execute(ns); 431 | 432 | // wait for subscriber to be ready. 433 | ns.waitUntilReady(); 434 | 435 | // let the connector start 436 | Thread.sleep(2000); 437 | 438 | // start the publisher 439 | executor.execute(rp); 440 | 441 | // wait for the subscriber to complete. 442 | ns.waitForCompletion(); 443 | 444 | Assert.assertTrue("Invalid count", ns.getMessageCount() == 5); 445 | 446 | c.shutdown(); 447 | } 448 | 449 | private void testOneToOneWithDefaultConfig(int count) throws Exception { 450 | 451 | System.clearProperty(RedisPubSubPlugin.CONFIG_URL); 452 | 453 | Connector c = new Connector(); 454 | 455 | ExecutorService executor = Executors.newFixedThreadPool(6); 456 | 457 | RedisSubscriber rs = new RedisSubscriber("rs", "Import_NATS", count); 458 | RedisPublisher rp = new RedisPublisher("rp", "Export_NATS", count); 459 | 460 | NatsPublisher np = new NatsPublisher("np", "Export.Redis", count); 461 | NatsSubscriber ns = new NatsSubscriber("ns", "Import.Redis", count); 462 | 463 | // start the connector 464 | executor.execute(c); 465 | 466 | // start the subsciber apps 467 | executor.execute(rs); 468 | executor.execute(ns); 469 | 470 | // wait for subscribers to be ready. 471 | rs.waitUntilReady(); 472 | ns.waitUntilReady(); 473 | 474 | // let the connector start 475 | Thread.sleep(1000); 476 | 477 | // start the publishers 478 | executor.execute(np); 479 | executor.execute(rp); 480 | 481 | // wait for the subscribers to complete. 482 | rs.waitForCompletion(); 483 | ns.waitForCompletion(); 484 | 485 | Assert.assertTrue("Invalid count", rs.getMessageCount() == count); 486 | Assert.assertTrue("Invalid count", ns.getMessageCount() == count); 487 | 488 | c.shutdown(); 489 | } 490 | 491 | @Test 492 | public void testBasicOneToOne() throws Exception { 493 | testOneToOneWithDefaultConfig(5); 494 | } 495 | 496 | // @Test 497 | public void testBasicOneToOneStress() throws Exception { 498 | testOneToOneWithDefaultConfig(50000); 499 | } 500 | 501 | @Test 502 | public void testNATSExportWildcard() throws Exception { 503 | String wildcardConfiguration = 504 | "{" + 505 | "\"nats_to_redis_map\" : [" + 506 | "{" + 507 | "\"subject\" : \"Export.*\"," + 508 | "\"channel\" : \"Import_NATS\"" + 509 | "}" + 510 | "]," + 511 | "\"redis_to_nats_map\" : [" + 512 | "{" + 513 | "\"channel\" : \"Export_NATS\"," + 514 | "\"subject\" : \"Import.Redis\"" + 515 | "}" + 516 | "]" + 517 | "}"; 518 | 519 | 520 | System.setProperty(RedisPubSubPlugin.CONFIG_URL, 521 | generateContentFile(wildcardConfiguration)); 522 | 523 | // success means it won't hang. 524 | new Connector().run(); 525 | } 526 | 527 | @Test 528 | public void testNATSImportWildcard() throws Exception { 529 | String wildcardConfiguration = 530 | "{" + 531 | "\"nats_to_redis_map\" : [" + 532 | "{" + 533 | "\"subject\" : \"Export.Redis\"," + 534 | "\"channel\" : \"Import_NATS\"" + 535 | "}" + 536 | "]," + 537 | "\"redis_to_nats_map\" : [" + 538 | "{" + 539 | "\"channel\" : \"Export_NATS\"," + 540 | "\"subject\" : \"Import.*\"" + 541 | "}" + 542 | "]" + 543 | "}"; 544 | 545 | 546 | System.setProperty(RedisPubSubPlugin.CONFIG_URL, 547 | generateContentFile(wildcardConfiguration)); 548 | 549 | // success means it won't hang. 550 | new Connector().run(); 551 | } 552 | 553 | @Test 554 | public void testCircularRouteDetection() throws Exception { 555 | String wildcardConfiguration = 556 | "{" + 557 | "\"nats_to_redis_map\" : [" + 558 | "{" + 559 | "\"subject\" : \"foo\"," + 560 | "\"channel\" : \"NATS\"" + 561 | "}" + 562 | "]," + 563 | "\"redis_to_nats_map\" : [" + 564 | "{" + 565 | "\"channel\" : \"NATS\"," + 566 | "\"subject\" : \"foo\"" + 567 | "}" + 568 | "]" + 569 | "}"; 570 | 571 | 572 | System.setProperty(RedisPubSubPlugin.CONFIG_URL, 573 | generateContentFile(wildcardConfiguration)); 574 | 575 | Connector c = new Connector(); 576 | 577 | ExecutorService executor = Executors.newFixedThreadPool(1); 578 | executor.execute(c); 579 | Thread.sleep(1000); 580 | c.shutdown(); 581 | 582 | // TODO - check logs for circular route message. 583 | } 584 | 585 | @Test 586 | public void testMulipleMappings() throws Exception { 587 | 588 | int count = 5; 589 | 590 | String config = 591 | "{" + 592 | "\"nats_to_redis_map\" : [" + 593 | "{" + 594 | "\"subject\" : \"Export.Redis\"," + 595 | "\"channel\" : \"Import_NATS\"" + 596 | "}," + 597 | "{" + 598 | "\"subject\" : \"Export.Redis2\"," + 599 | "\"channel\" : \"Import_NATS2\"" + 600 | "}" + 601 | "]," + 602 | "\"redis_to_nats_map\" : [" + 603 | "{" + 604 | "\"channel\" : \"Export_NATS\"," + 605 | "\"subject\" : \"Import.Redis\"" + 606 | "}," + 607 | "{" + 608 | "\"channel\" : \"Export_NATS2\"," + 609 | "\"subject\" : \"Import.Redis2\"" + 610 | "}" + 611 | "]" + 612 | "}"; 613 | 614 | 615 | System.setProperty(RedisPubSubPlugin.CONFIG_URL, 616 | generateContentFile(config)); 617 | 618 | Connector c = new Connector(); 619 | 620 | ExecutorService executor = Executors.newFixedThreadPool(10); 621 | 622 | RedisSubscriber rs1 = new RedisSubscriber("rs1", "Import_NATS", count); 623 | RedisPublisher rp1 = new RedisPublisher("rp1", "Export_NATS", count); 624 | RedisSubscriber rs2 = new RedisSubscriber("rs2", "Import_NATS2", count); 625 | RedisPublisher rp2 = new RedisPublisher("rp2", "Export_NATS2", count); 626 | 627 | NatsPublisher np1 = new NatsPublisher("np1", "Export.Redis", count); 628 | NatsSubscriber ns1 = new NatsSubscriber("ns1", "Import.Redis", count); 629 | NatsPublisher np2 = new NatsPublisher("np2", "Export.Redis2", count); 630 | NatsSubscriber ns2 = new NatsSubscriber("ns2", "Import.Redis2", count); 631 | 632 | // start the connector 633 | executor.execute(c); 634 | 635 | // start the subsciber apps 636 | executor.execute(rs1); 637 | executor.execute(rs2); 638 | executor.execute(ns1); 639 | executor.execute(ns2); 640 | 641 | // wait for subscribers to be ready. 642 | rs1.waitUntilReady(); 643 | rs2.waitUntilReady(); 644 | ns1.waitUntilReady(); 645 | ns2.waitUntilReady(); 646 | 647 | // let the connector start 648 | Thread.sleep(1000); 649 | 650 | // start the publishers 651 | executor.execute(np1); 652 | executor.execute(np2); 653 | executor.execute(rp1); 654 | executor.execute(rp2); 655 | 656 | // wait for the subscribers to complete. 657 | rs1.waitForCompletion(); 658 | rs2.waitForCompletion(); 659 | ns1.waitForCompletion(); 660 | ns2.waitForCompletion(); 661 | 662 | Assert.assertTrue("Invalid count", rs1.getMessageCount() == count); 663 | Assert.assertTrue("Invalid count", rs2.getMessageCount() == count); 664 | Assert.assertTrue("Invalid count", ns1.getMessageCount() == count); 665 | Assert.assertTrue("Invalid count", ns2.getMessageCount() == count); 666 | 667 | c.shutdown(); 668 | 669 | } 670 | 671 | /** 672 | * Test fanout NATS -> 2 Redis channels 673 | * Also tests one map present. 674 | * @throws Exception 675 | */ 676 | @Test 677 | public void testNatsSubjectFanoutToRedis() throws Exception { 678 | 679 | int count = 5; 680 | 681 | String config = 682 | "{" + 683 | "\"nats_to_redis_map\" : [" + 684 | "{" + 685 | "\"subject\" : \"Export.Redis\"," + 686 | "\"channel\" : \"Import_NATS\"" + 687 | "}," + 688 | "{" + 689 | "\"subject\" : \"Export.Redis\"," + 690 | "\"channel\" : \"Import_NATS2\"" + 691 | "}" + 692 | "]" + 693 | "}"; 694 | 695 | 696 | System.setProperty(RedisPubSubPlugin.CONFIG_URL, 697 | generateContentFile(config)); 698 | 699 | Connector c = new Connector(); 700 | 701 | ExecutorService executor = Executors.newFixedThreadPool(10); 702 | 703 | RedisSubscriber rs1 = new RedisSubscriber("rs1", "Import_NATS", count); 704 | RedisSubscriber rs2 = new RedisSubscriber("rs2", "Import_NATS2", count); 705 | 706 | NatsPublisher np1 = new NatsPublisher("np1", "Export.Redis", count); 707 | 708 | // start the connector 709 | executor.execute(c); 710 | 711 | // start the subsciber apps 712 | executor.execute(rs1); 713 | executor.execute(rs2); 714 | 715 | // wait for subscribers to be ready. 716 | rs1.waitUntilReady(); 717 | rs2.waitUntilReady(); 718 | 719 | // let the connector start 720 | Thread.sleep(1000); 721 | 722 | // start the publishers 723 | executor.execute(np1); 724 | 725 | // wait for the subscribers to complete. 726 | rs1.waitForCompletion(); 727 | rs2.waitForCompletion(); 728 | 729 | Assert.assertTrue("Invalid count", rs1.getMessageCount() == count); 730 | Assert.assertTrue("Invalid count", rs2.getMessageCount() == count); 731 | 732 | c.shutdown(); 733 | } 734 | 735 | @Test 736 | public void testRedisSubjectFanoutToNats() throws Exception { 737 | 738 | int count = 5; 739 | 740 | String config = 741 | "{" + 742 | "\"redis_to_nats_map\" : [" + 743 | "{" + 744 | "\"channel\" : \"Export_NATS\"," + 745 | "\"subject\" : \"Import.Redis\"" + 746 | "}," + 747 | "{" + 748 | "\"channel\" : \"Export_NATS\"," + 749 | "\"subject\" : \"Import.Redis2\"" + 750 | "}" + 751 | "]" + 752 | "}"; 753 | 754 | 755 | System.setProperty(RedisPubSubPlugin.CONFIG_URL, 756 | generateContentFile(config)); 757 | 758 | Connector c = new Connector(); 759 | 760 | ExecutorService executor = Executors.newFixedThreadPool(10); 761 | 762 | RedisPublisher rp1 = new RedisPublisher("rp1", "Export_NATS", count); 763 | 764 | NatsSubscriber ns1 = new NatsSubscriber("ns1", "Import.Redis", count); 765 | NatsSubscriber ns2 = new NatsSubscriber("ns2", "Import.Redis2", count); 766 | 767 | // start the connector 768 | executor.execute(c); 769 | 770 | // start the subsciber apps 771 | executor.execute(ns1); 772 | executor.execute(ns2); 773 | 774 | // wait for subscribers to be ready. 775 | ns1.waitUntilReady(); 776 | ns2.waitUntilReady(); 777 | 778 | // let the connector start 779 | Thread.sleep(1000); 780 | 781 | // start the publishers 782 | executor.execute(rp1); 783 | 784 | // wait for the subscribers to complete. 785 | ns1.waitForCompletion(); 786 | ns2.waitForCompletion(); 787 | 788 | Assert.assertTrue("Invalid count", ns1.getMessageCount() == count); 789 | Assert.assertTrue("Invalid count", ns2.getMessageCount() == count); 790 | 791 | c.shutdown(); 792 | 793 | } 794 | 795 | @Test 796 | public void testRedisSubjectFanIntoNats() throws Exception { 797 | 798 | int count = 500; 799 | 800 | String config = 801 | "{" + 802 | "\"redis_to_nats_map\" : [" + 803 | "{" + 804 | "\"channel\" : \"Export_NATS\"," + 805 | "\"subject\" : \"Import.Redis\"" + 806 | "}," + 807 | "{" + 808 | "\"channel\" : \"Export_NATS2\"," + 809 | "\"subject\" : \"Import.Redis\"" + 810 | "}," + 811 | "{" + 812 | "\"channel\" : \"Export_NATS3\"," + 813 | "\"subject\" : \"Import.Redis\"" + 814 | "}" + 815 | "]" + 816 | "}"; 817 | 818 | 819 | System.setProperty(RedisPubSubPlugin.CONFIG_URL, 820 | generateContentFile(config)); 821 | 822 | Connector c = new Connector(); 823 | 824 | ExecutorService executor = Executors.newFixedThreadPool(10); 825 | 826 | RedisPublisher rp1 = new RedisPublisher("rp1", "Export_NATS", count); 827 | RedisPublisher rp2 = new RedisPublisher("rp2", "Export_NATS2", count); 828 | RedisPublisher rp3 = new RedisPublisher("rp3", "Export_NATS3", count); 829 | 830 | NatsSubscriber ns1 = new NatsSubscriber("ns1", "Import.Redis", count*3); 831 | 832 | // start the connector 833 | executor.execute(c); 834 | 835 | // start the subsciber apps 836 | executor.execute(ns1); 837 | 838 | // wait for subscribers to be ready. 839 | ns1.waitUntilReady(); 840 | 841 | // let the connector start 842 | Thread.sleep(1000); 843 | 844 | // start the publishers 845 | executor.execute(rp1); 846 | executor.execute(rp2); 847 | executor.execute(rp3); 848 | 849 | // wait for the subscribers to complete. 850 | ns1.waitForCompletion(); 851 | 852 | Assert.assertTrue("Invalid count", ns1.getMessageCount() == (count*3)); 853 | 854 | c.shutdown(); 855 | 856 | Thread.sleep(2000); 857 | 858 | } 859 | 860 | @Test 861 | public void testNatsSubjectFanIntoRedis() throws Exception { 862 | 863 | int count = 5; 864 | 865 | String config = 866 | "{" + 867 | "\"nats_to_redis_map\" : [" + 868 | "{" + 869 | "\"subject\" : \"Export.Redis1\"," + 870 | "\"channel\" : \"Import_NATS\"" + 871 | "}," + 872 | "{" + 873 | "\"subject\" : \"Export.Redis2\"," + 874 | "\"channel\" : \"Import_NATS\"" + 875 | "}," + 876 | "{" + 877 | "\"subject\" : \"Export.Redis3\"," + 878 | "\"channel\" : \"Import_NATS\"" + 879 | "}" + 880 | "]" + 881 | "}"; 882 | 883 | 884 | System.setProperty(RedisPubSubPlugin.CONFIG_URL, 885 | generateContentFile(config)); 886 | 887 | Connector c = new Connector(); 888 | 889 | ExecutorService executor = Executors.newFixedThreadPool(10); 890 | 891 | NatsPublisher np1 = new NatsPublisher("np1", "Export.Redis1", count); 892 | NatsPublisher np2 = new NatsPublisher("np2", "Export.Redis2", count); 893 | NatsPublisher np3 = new NatsPublisher("np3", "Export.Redis3", count); 894 | 895 | RedisSubscriber rs1 = new RedisSubscriber("rs1", "Import_NATS", count*3); 896 | 897 | // start the subsciber apps 898 | executor.execute(rs1); 899 | 900 | // wait for subscribers to be ready. 901 | rs1.waitUntilReady(); 902 | 903 | // start the connector 904 | executor.execute(c); 905 | Thread.sleep(1000);; 906 | 907 | // start the publishers 908 | executor.execute(np1); 909 | executor.execute(np2); 910 | executor.execute(np3); 911 | 912 | // wait for the subscribers to complete. 913 | rs1.waitForCompletion(); 914 | 915 | Assert.assertTrue("Invalid count", rs1.getMessageCount() == (count*3)); 916 | 917 | c.shutdown(); 918 | 919 | } 920 | } -------------------------------------------------------------------------------- /src/test/java/io/nats/connector/plugins/redis/TestCasePrinterRule.java: -------------------------------------------------------------------------------- 1 | // Copyright 2016-2018 The NATS Authors 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | // 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | // 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | package io.nats.connector.plugins.redis; 14 | 15 | 16 | import org.junit.rules.ExternalResource; 17 | import org.junit.rules.TestRule; 18 | import org.junit.runner.Description; 19 | import org.junit.runners.model.Statement; 20 | 21 | import java.io.IOException; 22 | import java.io.OutputStream; 23 | import java.text.DecimalFormat; 24 | 25 | public class TestCasePrinterRule implements TestRule { 26 | 27 | private OutputStream out = null; 28 | private final TestCasePrinter printer = new TestCasePrinter(); 29 | 30 | private String beforeContent = null; 31 | private String afterContent = null; 32 | private long timeStart; 33 | private long timeEnd; 34 | 35 | public TestCasePrinterRule(OutputStream os) { 36 | out = os; 37 | } 38 | 39 | private class TestCasePrinter extends ExternalResource { 40 | @Override 41 | protected void before() throws Throwable { 42 | timeStart = System.currentTimeMillis(); 43 | out.write(beforeContent.getBytes()); 44 | }; 45 | 46 | @Override 47 | protected void after() { 48 | try { 49 | timeEnd = System.currentTimeMillis(); 50 | double seconds = (timeEnd-timeStart)/1000.0; 51 | out.write((afterContent+"Time elapsed: "+new DecimalFormat("0.000").format(seconds)+" sec\n").getBytes()); 52 | } catch (IOException ioe) { /* ignore */ 53 | } 54 | }; 55 | } 56 | 57 | public final Statement apply(Statement statement, Description description) { 58 | beforeContent = "\n[TEST START] "+description.getMethodName()+"\n"; // description.getClassName() to get class name 59 | afterContent = "[TEST ENDED] "; 60 | return printer.apply(statement, description); 61 | } 62 | } -------------------------------------------------------------------------------- /src/test/java/io/nats/connector/plugins/redis/UnitTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2016-2018 The NATS Authors 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | // 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | // 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | package io.nats.connector.plugins.redis; 15 | 16 | public interface UnitTest { 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/io/nats/connector/plugins/redis/UnitTestUtilities.java: -------------------------------------------------------------------------------- 1 | // Copyright 2016-2018 The NATS Authors 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | // 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | // 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | package io.nats.connector.plugins.redis; 15 | 16 | import io.nats.client.Connection; 17 | import io.nats.client.ConnectionFactory; 18 | 19 | import java.io.BufferedReader; 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | import java.io.InputStreamReader; 23 | import java.net.MalformedURLException; 24 | import java.net.URL; 25 | import java.util.concurrent.CountDownLatch; 26 | import java.util.concurrent.TimeUnit; 27 | import java.util.concurrent.TimeoutException; 28 | 29 | public class UnitTestUtilities { 30 | // final Object mu = new Object(); 31 | static NATSServer defaultServer = null; 32 | Process authServerProcess = null; 33 | 34 | static synchronized NATSServer runDefaultServer() { 35 | return runDefaultServer(false); 36 | } 37 | 38 | static synchronized NATSServer runDefaultServer(boolean debug) { 39 | NATSServer ns = new NATSServer(debug); 40 | sleep(100, TimeUnit.MILLISECONDS); 41 | return ns; 42 | } 43 | 44 | static synchronized Connection newDefaultConnection() throws IOException, TimeoutException { 45 | return new ConnectionFactory().createConnection(); 46 | } 47 | 48 | static synchronized void startDefaultServer() { 49 | startDefaultServer(false); 50 | } 51 | 52 | static synchronized void startDefaultServer(boolean debug) { 53 | if (defaultServer == null) { 54 | defaultServer = runDefaultServer(debug); 55 | } 56 | } 57 | 58 | static synchronized void stopDefaultServer() { 59 | if (defaultServer != null) { 60 | defaultServer.shutdown(); 61 | defaultServer = null; 62 | } 63 | } 64 | 65 | static synchronized void bounceDefaultServer(int delayMillis) { 66 | stopDefaultServer(); 67 | sleep(delayMillis); 68 | startDefaultServer(); 69 | } 70 | 71 | public void startAuthServer() throws IOException { 72 | authServerProcess = Runtime.getRuntime().exec("gnatsd -config auth.conf"); 73 | } 74 | 75 | static NATSServer runServerOnPort(int p) { 76 | return runServerOnPort(p, false); 77 | } 78 | 79 | static NATSServer runServerOnPort(int p, boolean debug) { 80 | NATSServer n = new NATSServer(p, debug); 81 | sleep(500); 82 | return n; 83 | } 84 | 85 | static NATSServer runServerWithConfig(String configFile) { 86 | return runServerWithConfig(configFile, false); 87 | } 88 | 89 | static NATSServer runServerWithConfig(String configFile, boolean debug) { 90 | NATSServer n = new NATSServer(configFile, debug); 91 | sleep(500); 92 | return n; 93 | } 94 | 95 | static String getCommandOutput(String command) { 96 | String output = null; // the string to return 97 | 98 | Process process = null; 99 | BufferedReader reader = null; 100 | InputStreamReader streamReader = null; 101 | InputStream stream = null; 102 | 103 | try { 104 | process = Runtime.getRuntime().exec(command); 105 | 106 | // Get stream of the console running the command 107 | stream = process.getInputStream(); 108 | streamReader = new InputStreamReader(stream); 109 | reader = new BufferedReader(streamReader); 110 | 111 | String currentLine = null; // store current line of output from the 112 | // cmd 113 | StringBuilder commandOutput = new StringBuilder(); // build up the 114 | // output from 115 | // cmd 116 | while ((currentLine = reader.readLine()) != null) { 117 | commandOutput.append(currentLine + "\n"); 118 | } 119 | 120 | int returnCode = process.waitFor(); 121 | if (returnCode == 0) { 122 | output = commandOutput.toString(); 123 | } 124 | 125 | } catch (IOException e) { 126 | System.err.println("Cannot retrieve output of command"); 127 | System.err.println(e); 128 | output = null; 129 | } catch (InterruptedException e) { 130 | System.err.println("Cannot retrieve output of command"); 131 | System.err.println(e); 132 | } finally { 133 | // Close all inputs / readers 134 | 135 | if (stream != null) { 136 | try { 137 | stream.close(); 138 | } catch (IOException e) { 139 | System.err.println("Cannot close stream input! " + e); 140 | } 141 | } 142 | if (streamReader != null) { 143 | try { 144 | streamReader.close(); 145 | } catch (IOException e) { 146 | System.err.println("Cannot close stream input reader! " + e); 147 | } 148 | } 149 | if (reader != null) { 150 | try { 151 | streamReader.close(); 152 | } catch (IOException e) { 153 | System.err.println("Cannot close stream input reader! " + e); 154 | } 155 | } 156 | } 157 | // Return the output from the command - may be null if an error occured 158 | return output; 159 | } 160 | 161 | void getConnz() { 162 | URL url = null; 163 | try { 164 | url = new URL("http://localhost:8222/connz"); 165 | } catch (MalformedURLException e) { 166 | // TODO Auto-generated catch block 167 | e.printStackTrace(); 168 | } 169 | 170 | try (BufferedReader reader = 171 | new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) { 172 | for (String line; (line = reader.readLine()) != null;) { 173 | System.out.println(line); 174 | } 175 | } catch (IOException e) { 176 | // TODO Auto-generated catch block 177 | e.printStackTrace(); 178 | } 179 | 180 | } 181 | 182 | static void sleep(int timeout) { 183 | sleep(timeout, TimeUnit.MILLISECONDS); 184 | } 185 | 186 | static void sleep(int duration, TimeUnit unit) { 187 | try { 188 | unit.sleep(duration); 189 | } catch (InterruptedException e) { 190 | /* NOOP */ 191 | } 192 | } 193 | 194 | static boolean await(CountDownLatch latch) { 195 | return await(latch, 5, TimeUnit.SECONDS); 196 | } 197 | 198 | static boolean await(CountDownLatch latch, long timeout, TimeUnit unit) { 199 | boolean val = false; 200 | try { 201 | val = latch.await(timeout, unit); 202 | } catch (InterruptedException e) { 203 | } 204 | return val; 205 | } 206 | 207 | 208 | // static synchronized void setLogLevel(ch.qos.logback.classic.Level level) { 209 | // ch.qos.logback.classic.Logger lbLog = 210 | // (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger("io.nats.client"); 211 | // lbLog.setLevel(level); 212 | // } 213 | } 214 | --------------------------------------------------------------------------------