├── .github └── workflows │ ├── build.yml │ └── maven-publish.yml ├── .gitignore ├── LICENSE ├── README.MD ├── pom.xml └── src ├── main └── java │ └── np │ └── com │ └── madanpokharel │ └── embed │ └── nats │ ├── EmbeddedNatsConfig.java │ ├── EmbeddedNatsServer.java │ ├── NatsPackageResolver.java │ ├── NatsProcess.java │ ├── NatsServerConfig.java │ ├── NatsServerStarter.java │ ├── NatsStarter.java │ ├── NatsStreamingVersion.java │ ├── NatsVersion.java │ ├── RunningNatsProcess.java │ └── ServerType.java └── test ├── java └── np │ └── com │ └── madanpokharel │ └── embed │ └── nats │ ├── EmbeddedNatsStreamingTest.java │ └── EmbeddedNatsTest.java └── resources └── logback.xml /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | paths: [ '**.java', '.github/workflows/build.yml', 'pom.xml' ] 7 | pull_request: 8 | branches: [ master ] 9 | workflow_dispatch: 10 | 11 | jobs: 12 | build: 13 | runs-on: ${{ matrix.os }} 14 | strategy: 15 | matrix: 16 | java: [ '11' ,'17', '21' ] 17 | os: [ 'ubuntu-latest', 'macos-latest', 'windows-latest' ] 18 | permissions: 19 | contents: read 20 | packages: write 21 | name: Java ${{ matrix.Java }} (${{ matrix.os }}) 22 | steps: 23 | - uses: actions/checkout@v4 24 | - name: Setup java 25 | uses: actions/setup-java@v4 26 | with: 27 | distribution: 'zulu' 28 | java-version: ${{ matrix.java }} 29 | - run: mvn test 30 | -------------------------------------------------------------------------------- /.github/workflows/maven-publish.yml: -------------------------------------------------------------------------------- 1 | name: Manual Maven Deploy Package 2 | 3 | on: 4 | workflow_dispatch: 5 | inputs: 6 | VERSION: 7 | description: 'New Version number' 8 | required: true 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | permissions: 14 | contents: read 15 | packages: write 16 | env: 17 | artifact_name: nats-embedded 18 | 19 | steps: 20 | - uses: actions/checkout@v4 21 | - name: Set up JDK 8 for deploy to OSSRH 22 | uses: actions/setup-java@v4 23 | with: 24 | distribution: 'temurin' 25 | java-version: '8' 26 | server-id: ossrh 27 | server-username: MAVEN_USERNAME 28 | server-password: MAVEN_CENTRAL_TOKEN 29 | gpg-private-key: ${{ secrets.GPG_SIGNING_KEY }} 30 | gpg-passphrase: MAVEN_GPG_PASSPHRASE 31 | 32 | - name: Build with Maven 33 | run: mvn -B package --file pom.xml 34 | 35 | - name: Update package version 36 | run: mvn versions:set -DnewVersion=${{ github.event.inputs.VERSION }} 37 | 38 | - name: Publish to Apache Maven Central 39 | run: mvn deploy -PmavenRelease 40 | env: 41 | MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} 42 | MAVEN_CENTRAL_TOKEN: ${{ secrets.OSSRH_PASSWORD }} 43 | MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} 44 | 45 | - name: Set up JDK 8 for deploy to github packages 46 | uses: actions/setup-java@v4 47 | with: 48 | distribution: 'temurin' 49 | java-version: '8' 50 | server-id: github 51 | 52 | - name: Publish to GitHub Packages Apache Maven 53 | run: mvn deploy -PgithubRelease 54 | env: 55 | GITHUB_TOKEN: ${{ secrets.NATS_GITHUB_TOKEN }} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/java,maven,intellij+all 3 | # Edit at https://www.gitignore.io/?templates=java,maven,intellij+all 4 | 5 | ### Intellij+all ### 6 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 7 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 8 | 9 | # User-specific stuff 10 | .idea/**/workspace.xml 11 | .idea/**/tasks.xml 12 | .idea/**/usage.statistics.xml 13 | .idea/**/dictionaries 14 | .idea/**/shelf 15 | 16 | # Generated files 17 | .idea/**/contentModel.xml 18 | 19 | # Sensitive or high-churn files 20 | .idea/**/dataSources/ 21 | .idea/**/dataSources.ids 22 | .idea/**/dataSources.local.xml 23 | .idea/**/sqlDataSources.xml 24 | .idea/**/dynamic.xml 25 | .idea/**/uiDesigner.xml 26 | .idea/**/dbnavigator.xml 27 | 28 | # Gradle 29 | .idea/**/gradle.xml 30 | .idea/**/libraries 31 | 32 | # Gradle and Maven with auto-import 33 | # When using Gradle or Maven with auto-import, you should exclude module files, 34 | # since they will be recreated, and may cause churn. Uncomment if using 35 | # auto-import. 36 | # .idea/modules.xml 37 | # .idea/*.iml 38 | # .idea/modules 39 | # *.iml 40 | # *.ipr 41 | 42 | # CMake 43 | cmake-build-*/ 44 | 45 | 46 | # File-based project format 47 | *.iws 48 | 49 | # IntelliJ 50 | out/ 51 | 52 | # mpeltonen/sbt-idea plugin 53 | .idea_modules/ 54 | 55 | # JIRA plugin 56 | atlassian-ide-plugin.xml 57 | 58 | # Cursive Clojure plugin 59 | .idea/replstate.xml 60 | 61 | # Crashlytics plugin (for Android Studio and IntelliJ) 62 | com_crashlytics_export_strings.xml 63 | crashlytics.properties 64 | crashlytics-build.properties 65 | fabric.properties 66 | 67 | # Editor-based Rest Client 68 | .idea/httpRequests 69 | 70 | # Android studio 3.1+ serialized cache file 71 | .idea/caches/build_file_checksums.ser 72 | 73 | ### Intellij+all Patch ### 74 | # Ignores the whole .idea folder and all .iml files 75 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 76 | 77 | .idea/ 78 | 79 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 80 | 81 | *.iml 82 | modules.xml 83 | .idea/misc.xml 84 | *.ipr 85 | 86 | # Sonarlint plugin 87 | .idea/sonarlint 88 | 89 | ### Java ### 90 | # Compiled class file 91 | *.class 92 | 93 | # Log file 94 | *.log 95 | 96 | # BlueJ files 97 | *.ctxt 98 | 99 | # Mobile Tools for Java (J2ME) 100 | .mtj.tmp/ 101 | 102 | # Package Files # 103 | *.jar 104 | *.war 105 | *.nar 106 | *.ear 107 | *.zip 108 | *.tar.gz 109 | *.rar 110 | 111 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 112 | hs_err_pid* 113 | 114 | ### Maven ### 115 | target/ 116 | pom.xml.tag 117 | pom.xml.releaseBackup 118 | pom.xml.versionsBackup 119 | pom.xml.next 120 | release.properties 121 | dependency-reduced-pom.xml 122 | buildNumber.properties 123 | .mvn/timing.properties 124 | .mvn/wrapper/maven-wrapper.jar 125 | .flattened-pom.xml 126 | 127 | # End of https://www.gitignore.io/api/java,maven,intellij+all -------------------------------------------------------------------------------- /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. -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/madansp/nats-embedded?style=for-the-badge&logo=appveyor) 2 | [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg?style=for-the-badge&logo=appveyor)](https://opensource.org/licenses/Apache-2.0) 3 | ![Maven Central](https://img.shields.io/maven-central/v/np.com.madanpokharel.embed/nats-embedded?style=for-the-badge&logo=appveyor) 4 | ### Embedded Nats and Streaming Sever 5 | 6 | Embedded Nats and Nats Streaming server for integration tests. 7 | Tested with Windows, Mac OSX and Linux 8 | 9 | ## Usage 10 | Add the following dependency to project 11 | For Maven: 12 | 13 | ```xml 14 | 15 | np.com.madanpokharel.embed 16 | nats-embedded 17 | 2.1.2 18 | test 19 | 20 | ``` 21 | For Gradle(Groovy Style): 22 | ``` groovy 23 | testCompile group: 'np.com.madanpokharel.embed', name: 'nats-embedded', version: '2.1.2' 24 | ``` 25 | 26 | For Gradle 27 | ```kotlin 28 | implementation("np.com.madanpokharel.embed:nats-embedded:2.1.2") 29 | ``` 30 | For Bazel 31 | ``` 32 | maven_jar( 33 | name = "nats-embedded", 34 | artifact = "np.com.madanpokharel.embed:nats-embedded:2.1.2", 35 | sha1 = "00f752b7b1c1758eb2f1aa35cf2352ba67d91710", 36 | ) 37 | ``` 38 | 39 | #### Example 40 | 41 | Start nats server with defined port 7656. 42 | 43 | ```java 44 | EmbeddedNatsConfig config = new EmbeddedNatsConfig.Builder() 45 | .withNatsServerConfig( 46 | new NatsServerConfig.Builder() 47 | .withServerType(ServerType.NATS) //Can be omitted, default is Nats 48 | .withPort(7656) 49 | .withNatsVersion(NatsVersion.LATEST) 50 | .build() 51 | ) 52 | .build(); 53 | EmbeddedNatsServer natsServer = new EmbeddedNatsServer(config); 54 | //start the server 55 | natsServer.startServer(); 56 | 57 | //get running port 58 | System.out.pritnln(natsServer.getRunningPort()) 59 | 60 | //stop the server 61 | natsServer.stopServer(); 62 | ``` 63 | 64 | Start with default settings. This will download latest version and start server on random port. 65 | 66 | ```java 67 | EmbeddedNatsConfig config = EmbeddedNatsConfig.defaultNatsServerConfig(); 68 | EmbeddedNatsServer natsServer = new EmbeddedNatsServer(config); 69 | natsServer.startServer(); 70 | 71 | //get running port 72 | System.out.println(natsServer.getRunningPort()) 73 | 74 | natsServer.stopServer(); 75 | 76 | ``` 77 | Start with custom config params 78 | 79 | ```java 80 | EmbeddedNatsConfig config = new EmbeddedNatsConfig.Builder() 81 | .withNatsServerConfig( 82 | new NatsServerConfig.Builder() 83 | .withServerType(ServerType.NATS_STREAMING) 84 | .withClusterId(clusterId) 85 | .withRandomPort() 86 | .withConfigParam("--store","FILE") //custom config parameter 87 | .withConfigParam("--dir","target/nats") //custom config parameter 88 | .withNatsStreamingVersion(NatsStreamingVersion.LATEST) 89 | .build() 90 | ) 91 | .build(); 92 | EmbeddedNatsServer natsStreamingServer = new EmbeddedNatsServer(config); 93 | natsStreamingServer.startServer(); 94 | ``` 95 | 96 | Customizing the download location and extraction path 97 | 98 | ```java 99 | EmbeddedNatsConfig config = new EmbeddedNatsConfig.Builder() 100 | .withNatsServerConfig( 101 | new NatsServerConfig.Builder() 102 | .withServerType(ServerType.NATS) 103 | .withPort(port) 104 | .withNatsVersion(NatsVersion.LATEST) 105 | .build() 106 | ) 107 | .withArtifactStorePath(".custom-download-dir") //custom download location 108 | .withExtractDirectory(".custom-extract-dir") // custom location for extraction of zip file 109 | .build(); 110 | ``` 111 | 112 | Example of Nats server with full configurations 113 | 114 | ```java 115 | EmbeddedNatsConfig config = new EmbeddedNatsConfig.Builder() 116 | .withNatsServerConfig( //configuration for nats server configuration 117 | new NatsServerConfig.Builder() 118 | .withServerType(ServerType.NATS) // server type either nats or nats streaming 119 | .withPort(2345) // port to run. can withRandomPort() if want to run on random port 120 | .withNatsVersion(NatsVersion.V2_1_0) // version to use 121 | .withHost("127.0.0.1") // host ip 122 | .withConfigParam("--store","FILE") // single config parameter 123 | .withConfigParams(Collections.singletonMap("--dir","target/nats")) // map of config parameters 124 | .build() 125 | ) 126 | .withArtifactStorePath(".custom-download-dir") // location for saving downloaded file 127 | .withExtractDirectory(".custom-extract-dir") // location for extracting downloaded zip file 128 | .build(); 129 | ``` 130 | Example of Nats Streaming server with full configurations 131 | ```java 132 | EmbeddedNatsConfig config = new EmbeddedNatsConfig.Builder() 133 | .withNatsServerConfig( //configuration for nats server configuration 134 | new NatsServerConfig.Builder() 135 | .withServerType(ServerType.NATS_STREAMING) // server type either nats or nats streaming 136 | .withPort(3445) // port to run. can withRandomPort() if want to run on random port 137 | .withNatsStreamingVersion(NatsStreamingVersion.V0_16_2) // version to use 138 | .withClusterId("clusterId") //cluster Id 139 | .withHost("127.0.0.1") // host ip 140 | .withConfigParam("--store","FILE") // single config parameter 141 | .withConfigParams(Collections.singletonMap("--dir","target/nats")) // map of config parameters 142 | .build() 143 | ) 144 | .withArtifactStorePath(".custom-download-dir") // location for saving downloaded file 145 | .withExtractDirectory(".custom-extract-dir") // location for extracting downloaded zip file 146 | .build(); 147 | 148 | ``` 149 | Using custom nats configuration file 150 | ```java 151 | new NatsServerConfig.Builder() 152 | .withConfigurationFile("custom-config-file.conf") 153 | .build() 154 | ``` 155 | Enabling JetStream 156 | ```java 157 | new NatsServerConfig.Builder() 158 | .withEnableJetStream() //without store location 159 | .build() 160 | 161 | new NatsServerConfig.Builder() 162 | .withEnableJetStream("target") //with store location 163 | .build() 164 | ``` 165 | ### Dependency 166 | This library using component from [flapdoodle-oss](https://github.com/flapdoodle-oss) 167 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 20 | 21 | 4.0.0 22 | 23 | nats-embedded 24 | Embedded Nats and Nats Streaming(https://nats.io) server for integration testing 25 | https://github.com/madansp/nats-embedded 26 | 27 | 28 | 29 | Apache-2.0 License 30 | https://opensource.org/licenses/Apache-2.0 31 | repo 32 | 33 | 34 | 35 | 36 | 37 | Madan Pokharel 38 | info@madanpokharel.com.np 39 | 40 | 41 | 42 | 43 | Github 44 | https://github.com/madansp/nats-embedded/issues 45 | 46 | 47 | 48 | Travis 49 | https://travis-ci.com/madansp/nats-embedded 50 | 51 | 52 | 53 | scm:git:https://github.com/madansp/nats-embedded.git 54 | scm:git:git@github.com:madansp/nats-embedded.git 55 | https://github.com/madansp/nats-embedded 56 | HEAD 57 | 58 | 59 | np.com.madanpokharel.embed 60 | nats-embedded 61 | 2.1.1 62 | jar 63 | 64 | 65 | UTF-8 66 | 1.8 67 | 1.8 68 | 69 | 4.13.2 70 | 2.16.1 71 | 2.20.0 72 | 2.2.3 73 | 74 | 4.13.2 75 | 1.3 76 | 3.22.0 77 | 1.7.2 78 | 79 | 80 | 81 | 82 | de.flapdoodle.embed 83 | de.flapdoodle.embed.process 84 | ${flapdoodle.version} 85 | 86 | 87 | 88 | de.flapdoodle 89 | de.flapdoodle.os 90 | ${de.flapdoodle.os.version} 91 | 92 | 93 | 94 | commons-io 95 | commons-io 96 | ${commons.io.version} 97 | 98 | 99 | 100 | io.nats 101 | jnats 102 | ${jnats.version} 103 | test 104 | 105 | 106 | 107 | io.nats 108 | java-nats-streaming 109 | ${jnats-streaming.version} 110 | test 111 | 112 | 113 | com.google.protobuf 114 | protobuf-java 115 | ${protobuf-java.version} 116 | test 117 | 118 | 119 | 120 | junit 121 | junit 122 | ${junit.version} 123 | test 124 | 125 | 126 | org.hamcrest 127 | hamcrest-all 128 | ${hamcrest.version} 129 | test 130 | 131 | 132 | 133 | 134 | 135 | mavenRelease 136 | 137 | 138 | 139 | org.apache.maven.plugins 140 | maven-source-plugin 141 | 3.3.1 142 | 143 | 144 | attach-sources 145 | 146 | jar-no-fork 147 | 148 | 149 | 150 | 151 | 152 | org.apache.maven.plugins 153 | maven-javadoc-plugin 154 | 3.8.0 155 | 156 | Madan Pokharel 157 | false 158 | 159 | 160 | 161 | attach-javadocs 162 | 163 | jar 164 | 165 | 166 | 167 | 168 | 169 | org.apache.maven.plugins 170 | maven-gpg-plugin 171 | 172 | 173 | sign-artifacts 174 | verify 175 | 176 | sign 177 | 178 | 179 | 180 | --pinentry-mode 181 | loopback 182 | 183 | 184 | 185 | 186 | 187 | 188 | org.sonatype.plugins 189 | nexus-staging-maven-plugin 190 | 1.7.0 191 | true 192 | 193 | ossrh 194 | https://oss.sonatype.org/ 195 | true 196 | 197 | 198 | 199 | org.apache.maven.plugins 200 | maven-compiler-plugin 201 | 3.13.0 202 | 203 | 204 | 205 | 206 | 207 | ossrh 208 | https://oss.sonatype.org/content/repositories/snapshots 209 | 210 | 211 | ossrh 212 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 213 | 214 | 215 | 216 | 217 | 218 | githubRelease 219 | 220 | 221 | github 222 | GitHub Nats Embedded package 223 | https://maven.pkg.github.com/madansp/nats-embedded 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/EmbeddedNatsConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 18 | 19 | import java.util.Objects; 20 | 21 | /** 22 | *

EmbeddedNatsConfig class.

23 | * 24 | * @author Madan Pokharel 25 | * @version $Id: $Id 26 | */ 27 | public final class EmbeddedNatsConfig { 28 | final String downloadPath; 29 | final String downloadUserAgent; 30 | final String artifactStorePath; 31 | final String extractDirectory; 32 | private final NatsServerConfig serverConfig; 33 | 34 | private EmbeddedNatsConfig(String downloadPath, 35 | String downloadUserAgent, 36 | String artifactStorePath, 37 | String extractDirectory, 38 | NatsServerConfig serverConfig) { 39 | this.downloadPath = downloadPath; 40 | this.downloadUserAgent = downloadUserAgent; 41 | this.serverConfig = serverConfig; 42 | this.artifactStorePath = artifactStorePath; 43 | this.extractDirectory = extractDirectory; 44 | } 45 | 46 | /** 47 | *

defaultNatsServerConfig.

48 | * 49 | * @return a {@link np.com.madanpokharel.embed.nats.EmbeddedNatsConfig} object. 50 | */ 51 | public static EmbeddedNatsConfig defaultNatsServerConfig() { 52 | return new EmbeddedNatsConfig.Builder() 53 | .withNatsServerConfig( 54 | new NatsServerConfig.Builder() 55 | .withServerType(ServerType.NATS) 56 | .withRandomPort() 57 | .build() 58 | ) 59 | .build(); 60 | } 61 | 62 | /** 63 | *

defaultNatsStreamingConfig.

64 | * 65 | * @return a {@link np.com.madanpokharel.embed.nats.EmbeddedNatsConfig} object. 66 | */ 67 | public static EmbeddedNatsConfig defaultNatsStreamingConfig() { 68 | return new EmbeddedNatsConfig.Builder() 69 | .withNatsServerConfig( 70 | new NatsServerConfig.Builder() 71 | .withServerType(ServerType.NATS_STREAMING) 72 | .withRandomPort() 73 | .build() 74 | ) 75 | .build(); 76 | } 77 | 78 | 79 | /** 80 | *

Getter for the field serverConfig.

81 | * 82 | * @return a {@link np.com.madanpokharel.embed.nats.NatsServerConfig} object. 83 | */ 84 | public NatsServerConfig getServerConfig() { 85 | return serverConfig; 86 | } 87 | 88 | public static class Builder { 89 | private String downloadUserAgent; 90 | private String artifactStorePath; 91 | private String extractDirectory; 92 | private NatsServerConfig serverConfig; 93 | 94 | public Builder() { 95 | this.artifactStorePath = ".embedded-nats"; 96 | this.extractDirectory = "extracted"; 97 | this.downloadUserAgent = "Mozilla/5.0 (compatible; Nats Embedded; +https://github.com/madansp/nats-embedded)"; 98 | } 99 | 100 | public Builder withDownloadUserAgent(String userAgent) { 101 | this.downloadUserAgent = userAgent; 102 | return this; 103 | } 104 | 105 | public Builder withArtifactStorePath(String artifactStorePath) { 106 | this.artifactStorePath = artifactStorePath; 107 | return this; 108 | } 109 | 110 | public Builder withExtractDirectory(String extractDirectory) { 111 | this.extractDirectory = extractDirectory; 112 | return this; 113 | } 114 | 115 | public Builder withNatsServerConfig(NatsServerConfig serverConfig) { 116 | this.serverConfig = serverConfig; 117 | return this; 118 | } 119 | 120 | public EmbeddedNatsConfig build() { 121 | Objects.requireNonNull(serverConfig, "Nats server config cannot be null"); 122 | String downloadPath = "https://github.com/nats-io/" + serverConfig.getServerType().getServerName() + "/releases/download/"; 123 | return new EmbeddedNatsConfig(downloadPath, downloadUserAgent, artifactStorePath, extractDirectory, serverConfig); 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/EmbeddedNatsServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 18 | 19 | import de.flapdoodle.embed.process.config.DownloadConfig; 20 | import de.flapdoodle.embed.process.config.SupportConfig; 21 | import de.flapdoodle.embed.process.config.store.Package; 22 | import de.flapdoodle.embed.process.distribution.ArchiveType; 23 | import de.flapdoodle.embed.process.distribution.Distribution; 24 | import de.flapdoodle.embed.process.distribution.Version; 25 | import de.flapdoodle.embed.process.io.ProcessOutput; 26 | import de.flapdoodle.embed.process.io.Processors; 27 | import de.flapdoodle.embed.process.io.directories.PersistentDir; 28 | import de.flapdoodle.embed.process.io.directories.TempDir; 29 | import de.flapdoodle.embed.process.io.progress.ProgressListener; 30 | import de.flapdoodle.embed.process.io.progress.StandardConsoleProgressListener; 31 | import de.flapdoodle.embed.process.store.ContentHashExtractedFileSetStore; 32 | import de.flapdoodle.embed.process.store.DownloadCache; 33 | import de.flapdoodle.embed.process.store.ExtractedFileSetStore; 34 | import de.flapdoodle.embed.process.store.LocalDownloadCache; 35 | import de.flapdoodle.embed.process.transitions.*; 36 | import de.flapdoodle.embed.process.types.*; 37 | import de.flapdoodle.os.CommonOS; 38 | import de.flapdoodle.os.Platform; 39 | import de.flapdoodle.reverse.StateID; 40 | import de.flapdoodle.reverse.TransitionWalker; 41 | import de.flapdoodle.reverse.Transitions; 42 | import de.flapdoodle.reverse.transitions.Derive; 43 | import de.flapdoodle.reverse.transitions.Start; 44 | 45 | import java.util.Collections; 46 | import java.util.Objects; 47 | 48 | 49 | /** 50 | *

EmbeddedNatsServer class.

51 | * 52 | * @author Madan Pokharel 53 | * @version $Id: $Id 54 | */ 55 | public final class EmbeddedNatsServer { 56 | private final EmbeddedNatsConfig config; 57 | 58 | private TransitionWalker.ReachedState runningProcess; 59 | 60 | /** 61 | *

Constructor for EmbeddedNatsServer.

62 | * 63 | * @param config a {@link np.com.madanpokharel.embed.nats.EmbeddedNatsConfig} object. 64 | */ 65 | public EmbeddedNatsServer(EmbeddedNatsConfig config) { 66 | Objects.requireNonNull(config, "config cannot be null"); 67 | this.config = config; 68 | } 69 | 70 | 71 | /** 72 | *

startServer.

73 | * 74 | * @throws java.lang.Exception if any. 75 | */ 76 | public void startServer() throws Exception { 77 | runningProcess = transitions().walker().initState(StateID.of(NatsProcess.class)); 78 | } 79 | 80 | 81 | /** 82 | *

isServerRunning.

83 | * 84 | * @return a boolean. 85 | */ 86 | public boolean isServerRunning() { 87 | return !Objects.isNull(runningProcess) && runningProcess.current().isAlive(); 88 | } 89 | 90 | 91 | /** 92 | *

stopServer.

93 | */ 94 | public void stopServer() { 95 | runningProcess.close(); 96 | } 97 | 98 | /** 99 | *

getRunningPort.

100 | * 101 | * @return an int. 102 | */ 103 | public int getRunningPort() { 104 | return config.getServerConfig().getPort(); 105 | } 106 | 107 | /** 108 | *

getRunningHost.

109 | * 110 | * @return a {@link java.lang.String} object. 111 | */ 112 | public String getRunningHost() { 113 | return config.getServerConfig().getHost(); 114 | } 115 | 116 | /** 117 | *

getNatsUrl.

118 | * 119 | * @return a {@link java.lang.String} object. 120 | */ 121 | public String getNatsUrl() { 122 | return "nats://" + getRunningHost() + ":" + getRunningPort(); 123 | } 124 | 125 | /** 126 | *

getClusterId.

127 | * 128 | * @return a {@link java.lang.String} object. 129 | */ 130 | public String getClusterId() { 131 | return this.config.getServerConfig().getClusterId(); 132 | } 133 | 134 | /** 135 | *

getServerConfig.

136 | * 137 | * @return a {@link np.com.madanpokharel.embed.nats.NatsServerConfig} object. 138 | */ 139 | public NatsServerConfig getServerConfig() { 140 | return this.config.getServerConfig(); 141 | } 142 | 143 | 144 | private Transitions transitions() { 145 | NatsPackageResolver resolver = new NatsPackageResolver(config.getServerConfig().getServerType()); 146 | Transitions transitions = Transitions.from( 147 | InitTempDirectory.withPlatformTempRandomSubDir(), 148 | 149 | Start.to(EmbeddedNatsConfig.class).providedBy(() -> config), 150 | 151 | Derive.given(EmbeddedNatsConfig.class) 152 | .state(PersistentDir.class) 153 | .deriveBy(embeddedNatsConfig -> PersistentDir.inUserHome(embeddedNatsConfig.artifactStorePath).mapToUncheckedException(RuntimeException::new).get()), 154 | 155 | Derive.given(TempDir.class) 156 | .state(ProcessWorkingDir.class) 157 | .with(Directories.deleteOnTearDown( 158 | TempDir.createDirectoryWith("workingDir"), 159 | ProcessWorkingDir::of) 160 | ), 161 | 162 | Derive.given(PersistentDir.class) 163 | .state(DownloadCache.class) 164 | .deriveBy(storeBaseDir -> new LocalDownloadCache(storeBaseDir.value().resolve("archives"))) 165 | .withTransitionLabel("downloadCache"), 166 | 167 | 168 | Derive.given(PersistentDir.class) 169 | .state(ExtractedFileSetStore.class) 170 | .deriveBy(baseDir -> new ContentHashExtractedFileSetStore(baseDir.value().resolve(config.extractDirectory))) 171 | .withTransitionLabel("extractedFileSetStore"), 172 | 173 | Start.to(Name.class).initializedWith(Name.of("nats")).withTransitionLabel("create Name"), 174 | 175 | Start.to(SupportConfig.class).initializedWith( 176 | SupportConfig.builder().name("nats-server") 177 | .supportUrl("https://github.com/madansp/nats-embedded") 178 | .messageOnException((clazz, ex) -> "Open a bug report at https://github.com/madansp/nats-embedded") 179 | .build() 180 | ).withTransitionLabel("create default"), 181 | 182 | Start.to(ProcessConfig.class).initializedWith(ProcessConfig.defaults()).withTransitionLabel("create default"), 183 | Start.to(ProcessEnv.class).initializedWith(ProcessEnv.of(Collections.emptyMap())).withTransitionLabel("create empty env"), 184 | 185 | Start.to(Version.class).initializedWith(config.getServerConfig().version()).withTransitionLabel("set version"), 186 | 187 | Derive.given(Name.class).state(ProcessOutput.class) 188 | .deriveBy(name -> 189 | ProcessOutput.builder() 190 | .output(Processors.silent()) 191 | .error(Processors.namedConsole("[" + name.value() + " output]")) 192 | .commands(Processors.console()) 193 | .build() 194 | ) 195 | .withTransitionLabel("create named console"), 196 | 197 | Start.to(ProgressListener.class) 198 | .providedBy(StandardConsoleProgressListener::new) 199 | .withTransitionLabel("progressListener"), 200 | 201 | Start.to(ProcessArguments.class).initializedWith(ProcessArguments.of(config.getServerConfig().getConfigList())) 202 | .withTransitionLabel("create arguments"), 203 | 204 | Start.to(ServerType.class).providedBy(() -> config.getServerConfig().getServerType()), 205 | 206 | Derive.given(Version.class).state(Distribution.class) 207 | .deriveBy(version -> Distribution.detectFor(CommonOS.list(), version)) 208 | .withTransitionLabel("version + platform"), 209 | 210 | Start.to(Platform.class).providedBy(() -> Platform.detect(CommonOS.list())), 211 | 212 | PackageOfDistribution.with(dist -> Package.builder() 213 | .archiveType(ArchiveType.ZIP) 214 | .fileSet(resolver.getExecutableFileSet(dist)) 215 | .url(config.downloadPath + resolver.getPath(dist)) 216 | .build()), 217 | 218 | DownloadPackage.builder() 219 | .downloadConfig(DownloadConfig.builder().userAgent(config.downloadUserAgent).build()) 220 | .build(), 221 | 222 | ExtractPackage.withDefaults().withExtractedFileSetStore(StateID.of(ExtractedFileSetStore.class)), 223 | 224 | NatsStarter.defaults() 225 | ); 226 | 227 | return transitions; 228 | } 229 | 230 | } 231 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/NatsPackageResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 18 | 19 | import de.flapdoodle.embed.process.config.store.FileSet; 20 | import de.flapdoodle.embed.process.config.store.FileType; 21 | import de.flapdoodle.embed.process.distribution.Distribution; 22 | import de.flapdoodle.os.BitSize; 23 | import de.flapdoodle.os.Platform; 24 | 25 | final class NatsPackageResolver { 26 | private final ServerType serverType; 27 | 28 | /** 29 | *

Constructor for NatsPackageResolver.

30 | * 31 | * @param serverType a {@link np.com.madanpokharel.embed.nats.ServerType} object. 32 | */ 33 | NatsPackageResolver(ServerType serverType) { 34 | this.serverType = serverType; 35 | } 36 | 37 | /** 38 | * {@inheritDoc} 39 | * 40 | * @param distribution a {@link de.flapdoodle.embed.process.distribution.Distribution} object 41 | * @return a {@link java.lang.String} object 42 | */ 43 | public String getPath(Distribution distribution) { 44 | String version = distribution.version().asInDownloadPath(); 45 | String arch = getArch(distribution.platform().architecture().bitSize()); 46 | String platform = getPlatform(distribution.platform()); 47 | return version + "/" + serverType.getServerName() + "-" + version + '-' + platform + '-' + arch + ".zip"; 48 | } 49 | 50 | private String getPlatform(Platform platform) { 51 | switch (platform.operatingSystem().type()) { 52 | case OS_X: 53 | return "darwin"; 54 | case Linux: 55 | return "linux"; 56 | case Windows: 57 | return "windows"; 58 | default: 59 | throw new IllegalArgumentException("Unknown platform " + platform); 60 | } 61 | } 62 | 63 | private String getArch(BitSize bitSize) { 64 | String arch; 65 | if (System.getProperty("os.arch").startsWith("arm")) { 66 | switch (bitSize) { 67 | case B32: 68 | arch = "arm7"; 69 | break; 70 | case B64: 71 | arch = "arm64"; 72 | break; 73 | default: 74 | arch = "arm6"; 75 | } 76 | } else { 77 | switch (bitSize) { 78 | case B32: 79 | arch = "386"; 80 | break; 81 | case B64: 82 | arch = "amd64"; 83 | break; 84 | default: 85 | throw new IllegalArgumentException("Unknown bit size " + bitSize); 86 | } 87 | } 88 | 89 | return arch; 90 | } 91 | 92 | FileSet getExecutableFileSet(Distribution distribution) { 93 | String executableName = serverType.getServerName(); 94 | switch (distribution.platform().operatingSystem().type()) { 95 | case Linux: 96 | case OS_X: 97 | break; 98 | case Windows: 99 | executableName += ".exe"; 100 | break; 101 | default: 102 | throw new IllegalArgumentException( 103 | "Unknown platform " + distribution.platform()); 104 | } 105 | return FileSet.builder() 106 | .addEntry(FileType.Executable, executableName) 107 | .build(); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/NatsProcess.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 17 | 18 | import de.flapdoodle.embed.process.config.SupportConfig; 19 | import de.flapdoodle.embed.process.io.StreamProcessor; 20 | import de.flapdoodle.embed.process.runtime.ProcessControl; 21 | import de.flapdoodle.embed.process.types.RunningProcessFactory; 22 | import de.flapdoodle.os.Platform; 23 | 24 | import java.nio.file.Path; 25 | 26 | /** 27 | *

NatsProcess class.

28 | * 29 | * @author madanpokharel 30 | * @version $Id: $Id 31 | */ 32 | public class NatsProcess extends RunningNatsProcess { 33 | 34 | /** 35 | *

Constructor for NatsProcess.

36 | * 37 | * @param process a {@link de.flapdoodle.embed.process.runtime.ProcessControl} object 38 | * @param pidFile a {@link java.nio.file.Path} object 39 | * @param onStop a {@link java.lang.Runnable} object 40 | * @param supportConfig a {@link de.flapdoodle.embed.process.config.SupportConfig} object 41 | * @param platform a {@link de.flapdoodle.os.Platform} object 42 | * @param commandOutput a {@link de.flapdoodle.embed.process.io.StreamProcessor} object 43 | * @param natsProcessId a int 44 | * @param serverType a {@link np.com.madanpokharel.embed.nats.ServerType} object 45 | */ 46 | public NatsProcess( 47 | ProcessControl process, 48 | Path pidFile, 49 | Runnable onStop, 50 | SupportConfig supportConfig, 51 | Platform platform, 52 | StreamProcessor commandOutput, 53 | int natsProcessId, 54 | ServerType serverType 55 | ) { 56 | super("natsProcess", process, pidFile, onStop, supportConfig, platform, commandOutput, natsProcessId, serverType); 57 | } 58 | 59 | /** 60 | *

factory.

61 | * 62 | * @param supportConfig a {@link de.flapdoodle.embed.process.config.SupportConfig} object 63 | * @param platform a {@link de.flapdoodle.os.Platform} object 64 | * @param serverType a {@link np.com.madanpokharel.embed.nats.ServerType} object 65 | * @return a {@link de.flapdoodle.embed.process.types.RunningProcessFactory} object 66 | */ 67 | public static RunningProcessFactory factory(SupportConfig supportConfig, Platform platform, ServerType serverType) { 68 | return RunningNatsProcess.factory(NatsProcess::new, supportConfig, platform, serverType); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/NatsServerConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 18 | 19 | import de.flapdoodle.embed.process.config.SupportConfig; 20 | import de.flapdoodle.embed.process.distribution.Version; 21 | import de.flapdoodle.embed.process.runtime.Network; 22 | import de.flapdoodle.net.Net; 23 | import org.apache.commons.lang3.StringUtils; 24 | 25 | import java.io.IOException; 26 | import java.net.InetAddress; 27 | import java.util.*; 28 | 29 | /** 30 | *

NatsServerConfig class.

31 | * 32 | * @author Madan Pokharel 33 | * @version $Id: $Id 34 | */ 35 | public final class NatsServerConfig { 36 | 37 | private final Version version; 38 | private final int port; 39 | private final String host; 40 | private final String clusterId; 41 | private final ServerType serverType; 42 | private final Map params = new HashMap<>(); 43 | 44 | 45 | /** 46 | *

Constructor for NatsServerConfig.

47 | * 48 | * @param version a {@link de.flapdoodle.embed.process.distribution.Version} object. 49 | * @param port an int. 50 | * @param host a {@link java.lang.String} object. 51 | * @param clusterId a {@link java.lang.String} object. 52 | * @param serverType a {@link np.com.madanpokharel.embed.nats.ServerType} object. 53 | * @param configParams a {@link java.util.Map} object. 54 | */ 55 | public NatsServerConfig(Version version, 56 | int port, 57 | String host, 58 | String clusterId, 59 | ServerType serverType, 60 | Map configParams) { 61 | this.version = version; 62 | this.port = port; 63 | this.host = host; 64 | this.clusterId = clusterId; 65 | this.serverType = serverType; 66 | this.params.putAll(configParams); 67 | } 68 | 69 | /** 70 | *

Getter for the field port.

71 | * 72 | * @return an int. 73 | */ 74 | public int getPort() { 75 | return port; 76 | } 77 | 78 | /** 79 | * {@inheritDoc} 80 | * 81 | * @return a {@link de.flapdoodle.embed.process.distribution.Version} object 82 | */ 83 | public Version version() { 84 | return version; 85 | } 86 | 87 | 88 | /** 89 | *

getConfigList.

90 | * 91 | * @return a {@link java.util.List} object. 92 | */ 93 | List getConfigList() { 94 | List configMap = new ArrayList<>(); 95 | 96 | configMap.add("--port"); 97 | configMap.add(Integer.toString(port)); 98 | 99 | configMap.add("--addr"); 100 | configMap.add(host); 101 | 102 | if (StringUtils.isNotBlank(clusterId)) { 103 | configMap.add("-cid"); 104 | configMap.add(clusterId); 105 | } 106 | 107 | params.forEach((k, v) -> { 108 | configMap.add(k); 109 | if (!v.isEmpty()) { 110 | configMap.add(v); 111 | } 112 | }); 113 | 114 | return configMap; 115 | } 116 | 117 | /** 118 | *

Getter for the field host.

119 | * 120 | * @return a {@link java.lang.String} object. 121 | */ 122 | public String getHost() { 123 | return host; 124 | } 125 | 126 | /** 127 | *

Getter for the field clusterId.

128 | * 129 | * @return a {@link java.lang.String} object. 130 | */ 131 | public String getClusterId() { 132 | return clusterId; 133 | } 134 | 135 | /** 136 | *

Getter for the field serverType.

137 | * 138 | * @return a {@link np.com.madanpokharel.embed.nats.ServerType} object. 139 | */ 140 | public ServerType getServerType() { 141 | return serverType; 142 | } 143 | 144 | public static class Builder { 145 | private Version version; 146 | private int port; 147 | private String host; 148 | private String clusterId; 149 | private ServerType serverType; 150 | private final Map configParams = new HashMap<>(); 151 | 152 | public Builder() { 153 | this.port = 4222; 154 | this.host = "0.0.0.0"; 155 | } 156 | 157 | public Builder withRandomPort() { 158 | try { 159 | InetAddress localHost = Net.getLocalHost(); 160 | this.port = Net.freeServerPort(localHost); 161 | } catch (IOException ex) { 162 | throw new RuntimeException("unbale to allocate random port"); 163 | } 164 | return this; 165 | } 166 | 167 | public Builder withPort(int port) { 168 | this.port = port; 169 | return this; 170 | } 171 | 172 | public Builder withHost(String host) { 173 | this.host = host; 174 | return this; 175 | } 176 | 177 | public Builder withClusterId(String clusterId) { 178 | this.clusterId = clusterId; 179 | return this; 180 | } 181 | 182 | public Builder withServerType(ServerType serverType) { 183 | this.serverType = serverType; 184 | return this; 185 | } 186 | 187 | public Builder withNatsVersion(NatsVersion version) { 188 | this.version = version; 189 | return this; 190 | } 191 | 192 | public Builder withNatsStreamingVersion(NatsStreamingVersion natsStreamingVersion) { 193 | this.version = natsStreamingVersion; 194 | return this; 195 | } 196 | 197 | public Builder withConfigParams(Map params) { 198 | this.configParams.putAll(params); 199 | return this; 200 | } 201 | 202 | public Builder withConfigParam(String param, String value) { 203 | this.configParams.put(param, value); 204 | return this; 205 | } 206 | 207 | public Builder withEnableJetStream(String storeDir) { 208 | configParams.put("-js", ""); 209 | configParams.put("-sd", storeDir); 210 | return this; 211 | } 212 | 213 | public Builder withEnableJetStream() { 214 | configParams.put("-js", ""); 215 | return this; 216 | } 217 | 218 | public Builder withConfigurationFile(String filePath) { 219 | configParams.put("-c", filePath); 220 | return this; 221 | } 222 | 223 | 224 | public NatsServerConfig build() { 225 | if (serverType == null) { 226 | serverType = ServerType.NATS; 227 | } 228 | 229 | if (Objects.isNull(version)) { 230 | if (serverType == ServerType.NATS) { 231 | this.version = NatsVersion.LATEST; 232 | } else { 233 | this.version = NatsStreamingVersion.LATEST; 234 | } 235 | } 236 | 237 | if (serverType == ServerType.NATS) { 238 | if (StringUtils.isNotBlank(clusterId)) { 239 | throw new IllegalArgumentException("cluster id is not applicable for nats server"); 240 | } 241 | 242 | if (!(version instanceof NatsVersion)) { 243 | throw new IllegalArgumentException("version do not match server type"); 244 | } 245 | } 246 | 247 | if (serverType == ServerType.NATS_STREAMING && !(version instanceof NatsStreamingVersion)) { 248 | throw new IllegalArgumentException("version do not match server type"); 249 | } 250 | 251 | return new NatsServerConfig(version, port, host, clusterId, serverType, configParams); 252 | } 253 | } 254 | } 255 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/NatsServerStarter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 17 | 18 | import de.flapdoodle.embed.process.archives.ExtractedFileSet; 19 | import de.flapdoodle.embed.process.config.SupportConfig; 20 | import de.flapdoodle.embed.process.io.ProcessOutput; 21 | import de.flapdoodle.embed.process.types.*; 22 | import de.flapdoodle.os.Platform; 23 | import de.flapdoodle.reverse.State; 24 | import de.flapdoodle.reverse.StateID; 25 | import de.flapdoodle.reverse.StateLookup; 26 | import de.flapdoodle.reverse.Transition; 27 | 28 | import java.io.IOException; 29 | import java.nio.file.Path; 30 | import java.util.List; 31 | import java.util.Map; 32 | import java.util.Set; 33 | 34 | /** 35 | *

Abstract NatsServerStarter class.

36 | * 37 | * @author madanpokharel 38 | * @version $Id: $Id 39 | */ 40 | public abstract class NatsServerStarter implements Transition { 41 | /** 42 | *

processExecutable.

43 | * 44 | * @return a {@link de.flapdoodle.reverse.StateID} object 45 | */ 46 | public StateID processExecutable() { 47 | return StateID.of(ExtractedFileSet.class); 48 | } 49 | 50 | /** 51 | *

processWorkingDir.

52 | * 53 | * @return a {@link de.flapdoodle.reverse.StateID} object 54 | */ 55 | public StateID processWorkingDir() { 56 | return StateID.of(ProcessWorkingDir.class); 57 | } 58 | 59 | /** 60 | *

processConfig.

61 | * 62 | * @return a {@link de.flapdoodle.reverse.StateID} object 63 | */ 64 | public StateID processConfig() { 65 | return StateID.of(ProcessConfig.class); 66 | } 67 | 68 | /** 69 | *

processEnv.

70 | * 71 | * @return a {@link de.flapdoodle.reverse.StateID} object 72 | */ 73 | public StateID processEnv() { 74 | return StateID.of(ProcessEnv.class); 75 | } 76 | 77 | /** 78 | *

arguments.

79 | * 80 | * @return a {@link de.flapdoodle.reverse.StateID} object 81 | */ 82 | public StateID arguments() { 83 | return StateID.of(ProcessArguments.class); 84 | } 85 | 86 | /** 87 | *

processOutput.

88 | * 89 | * @return a {@link de.flapdoodle.reverse.StateID} object 90 | */ 91 | public StateID processOutput() { 92 | return StateID.of(ProcessOutput.class); 93 | } 94 | 95 | /** 96 | *

supportConfig.

97 | * 98 | * @return a {@link de.flapdoodle.reverse.StateID} object 99 | */ 100 | public StateID supportConfig() { 101 | return StateID.of(SupportConfig.class); 102 | } 103 | 104 | /** 105 | *

platform.

106 | * 107 | * @return a {@link de.flapdoodle.reverse.StateID} object 108 | */ 109 | public StateID platform() { 110 | return StateID.of(Platform.class); 111 | } 112 | 113 | /** 114 | *

serverType.

115 | * 116 | * @return a {@link de.flapdoodle.reverse.StateID} object 117 | */ 118 | public StateID serverType() { 119 | return StateID.of(ServerType.class); 120 | } 121 | 122 | 123 | /** {@inheritDoc} */ 124 | @Override 125 | public Set> sources() { 126 | return StateID.setOf( 127 | processWorkingDir(), 128 | processExecutable(), 129 | processConfig(), 130 | processEnv(), 131 | arguments(), 132 | processOutput(), 133 | supportConfig(), 134 | platform(), 135 | serverType() 136 | ); 137 | } 138 | 139 | /** 140 | *

factory.

141 | * 142 | * @param supportConfig a {@link de.flapdoodle.embed.process.config.SupportConfig} object 143 | * @param platform a {@link de.flapdoodle.os.Platform} object 144 | * @param serverType a {@link np.com.madanpokharel.embed.nats.ServerType} object 145 | * @return a {@link de.flapdoodle.embed.process.types.RunningProcessFactory} object 146 | */ 147 | protected abstract RunningProcessFactory factory(SupportConfig supportConfig, Platform platform, ServerType serverType); 148 | 149 | /** {@inheritDoc} */ 150 | @Override 151 | public State result(StateLookup lookup) { 152 | Path processWorkingDir = lookup.of(processWorkingDir()).value(); 153 | ExtractedFileSet fileSet = lookup.of(processExecutable()); 154 | List arguments = lookup.of(arguments()).value(); 155 | Map environment = lookup.of(processEnv()).value(); 156 | ProcessConfig processConfig = lookup.of(processConfig()); 157 | ProcessOutput processOutput = lookup.of(processOutput()); 158 | SupportConfig supportConfig = lookup.of(supportConfig()); 159 | Platform platform = lookup.of(platform()); 160 | ServerType serverType = lookup.of(serverType()); 161 | 162 | try { 163 | RunningProcessFactory factory = factory(supportConfig, platform, serverType); 164 | 165 | T running = RunningProcess.start(factory, processWorkingDir, fileSet.executable(), arguments, environment, processConfig, 166 | processOutput, supportConfig); 167 | 168 | return State.of(running, RunningProcess::stop); 169 | } catch (IOException ix) { 170 | String hint = ""; 171 | if (ix.getMessage().contains("Bad CPU type in executable")) { 172 | hint = " - " + platform.toString(); 173 | } 174 | throw new RuntimeException("could not start process" + hint, ix); 175 | } 176 | } 177 | 178 | } 179 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/NatsStarter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 17 | 18 | import de.flapdoodle.embed.process.config.SupportConfig; 19 | import de.flapdoodle.embed.process.types.RunningProcessFactory; 20 | import de.flapdoodle.os.Platform; 21 | import de.flapdoodle.reverse.StateID; 22 | 23 | /** 24 | *

NatsStarter class.

25 | * 26 | * @author madanpokharel 27 | * @version $Id: $Id 28 | */ 29 | public class NatsStarter extends NatsServerStarter { 30 | /** {@inheritDoc} */ 31 | @Override 32 | public StateID destination() { 33 | return StateID.of(NatsProcess.class); 34 | } 35 | 36 | 37 | static NatsStarter defaults() { 38 | return new NatsStarter(); 39 | } 40 | 41 | /** {@inheritDoc} */ 42 | @Override 43 | protected RunningProcessFactory factory(SupportConfig supportConfig, Platform platform, ServerType serverType) { 44 | return NatsProcess.factory(supportConfig, platform, serverType); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/NatsStreamingVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 18 | 19 | import de.flapdoodle.embed.process.distribution.Version; 20 | 21 | /** 22 | *

NatsStreamingVersion class.

23 | * 24 | * @author Madan Pokharel 25 | * @version $Id: $Id 26 | */ 27 | public class NatsStreamingVersion implements Version { 28 | /** 29 | * Constant LATEST 30 | */ 31 | public static final NatsStreamingVersion LATEST = new NatsStreamingVersion("v0.25.6"); 32 | 33 | /** 34 | * Constant V0_25_6 35 | */ 36 | public static final NatsStreamingVersion V0_25_6 = new NatsStreamingVersion("v0.25.6"); 37 | 38 | /** 39 | * Constant V0_25_5 40 | */ 41 | public static final NatsStreamingVersion V0_25_5 = new NatsStreamingVersion("v0.25.5"); 42 | 43 | /** 44 | * Constant V0_25_4 45 | */ 46 | public static final NatsStreamingVersion V0_25_4 = new NatsStreamingVersion("v0.25.4"); 47 | 48 | /** 49 | * Constant V0_25_3 50 | */ 51 | public static final NatsStreamingVersion V0_25_3 = new NatsStreamingVersion("v0.25.3"); 52 | 53 | /** 54 | * Constant V0_25_2 55 | */ 56 | public static final NatsStreamingVersion V0_25_2 = new NatsStreamingVersion("v0.25.2"); 57 | 58 | /** 59 | * Constant V0_24_6 60 | */ 61 | public static final NatsStreamingVersion V0_24_6 = new NatsStreamingVersion("v0.24.6"); 62 | 63 | /** 64 | * Constant V0_24_5 65 | */ 66 | public static final NatsStreamingVersion V0_24_5 = new NatsStreamingVersion("v0.24.5"); 67 | 68 | /** 69 | * Constant V0_24_4 70 | */ 71 | public static final NatsStreamingVersion V0_24_4 = new NatsStreamingVersion("v0.24.4"); 72 | 73 | /** 74 | * Constant V0_24_3 75 | */ 76 | public static final NatsStreamingVersion V0_24_3 = new NatsStreamingVersion("v0.24.3"); 77 | 78 | /** 79 | * Constant V0_24_2 80 | */ 81 | public static final NatsStreamingVersion V0_24_2 = new NatsStreamingVersion("v0.24.2"); 82 | 83 | /** 84 | * Constant V0_24_1 85 | */ 86 | public static final NatsStreamingVersion V0_24_1 = new NatsStreamingVersion("v0.24.1"); 87 | 88 | /** 89 | * Constant V0_24_0 90 | */ 91 | public static final NatsStreamingVersion V0_24_0 = new NatsStreamingVersion("v0.24.0"); 92 | 93 | /** 94 | * Constant V0_23_2 95 | */ 96 | public static final NatsStreamingVersion V0_23_2 = new NatsStreamingVersion("v0.23.2"); 97 | 98 | /** 99 | * Constant V0_23_1 100 | */ 101 | public static final NatsStreamingVersion V0_23_1 = new NatsStreamingVersion("v0.23.1"); 102 | 103 | /** 104 | * Constant V0_23_0 105 | */ 106 | public static final NatsStreamingVersion V0_23_0 = new NatsStreamingVersion("v0.23.0"); 107 | 108 | /** 109 | * Constant V0_22_1 110 | */ 111 | public static final NatsStreamingVersion V0_22_1 = new NatsStreamingVersion("v0.21.1"); 112 | 113 | /** 114 | * Constant V0_22_0 115 | */ 116 | public static final NatsStreamingVersion V0_22_0 = new NatsStreamingVersion("v0.22.0"); 117 | 118 | /** 119 | * Constant V0_21_2 120 | */ 121 | public static final NatsStreamingVersion V0_21_2 = new NatsStreamingVersion("v0.21.2"); 122 | 123 | /** 124 | * Constant V0_21_1 125 | */ 126 | public static final NatsStreamingVersion V0_21_1 = new NatsStreamingVersion("v0.21.1"); 127 | 128 | /** 129 | * Constant V0_21_0 130 | */ 131 | public static final NatsStreamingVersion V0_21_0 = new NatsStreamingVersion("v0.21.0"); 132 | 133 | /** 134 | * Constant V0_20_0 135 | */ 136 | public static final NatsStreamingVersion V0_20_0 = new NatsStreamingVersion("v0.20.0"); 137 | 138 | /** 139 | * Constant V0_19_0 140 | */ 141 | public static final NatsStreamingVersion V0_19_0 = new NatsStreamingVersion("v0.19.0"); 142 | 143 | /** 144 | * Constant V0_18_0 145 | */ 146 | public static final NatsStreamingVersion V0_18_0 = new NatsStreamingVersion("v0.18.0"); 147 | 148 | 149 | /** 150 | * Constant V0_17_0 151 | */ 152 | public static final NatsStreamingVersion V0_17_0 = new NatsStreamingVersion("v0.17.0"); 153 | 154 | /** 155 | * Constant V0_16_2 156 | */ 157 | public static final NatsStreamingVersion V0_16_2 = new NatsStreamingVersion("v0.16.2"); 158 | /** 159 | * Constant V0_16_0 160 | */ 161 | public static final NatsStreamingVersion V0_16_0 = new NatsStreamingVersion("v0.16.0"); 162 | /** 163 | * Constant V0_15_1 164 | */ 165 | public static final NatsStreamingVersion V0_15_1 = new NatsStreamingVersion("v0.15.1"); 166 | /** 167 | * Constant V0_15_0 168 | */ 169 | public static final NatsStreamingVersion V0_15_0 = new NatsStreamingVersion("v0.15.0"); 170 | /** 171 | * Constant V0_14_3 172 | */ 173 | public static final NatsStreamingVersion V0_14_3 = new NatsStreamingVersion("v0.14.3"); 174 | 175 | private final String version; 176 | 177 | /** 178 | *

Constructor for NatsStreamingVersion.

179 | * 180 | * @param version a {@link java.lang.String} object. 181 | */ 182 | public NatsStreamingVersion(String version) { 183 | this.version = version; 184 | } 185 | 186 | /** {@inheritDoc} */ 187 | @Override 188 | public String asInDownloadPath() { 189 | return version; 190 | } 191 | 192 | /** {@inheritDoc} */ 193 | @Override 194 | public String toString() { 195 | return version; 196 | } 197 | 198 | } 199 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/NatsVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 18 | 19 | import de.flapdoodle.embed.process.distribution.Version; 20 | 21 | /** 22 | *

NatsVersion class.

23 | * 24 | * @author Madan Pokharel 25 | * @version $Id: $Id 26 | */ 27 | public class NatsVersion implements Version { 28 | /** 29 | * Constant LATEST 30 | */ 31 | public static final NatsVersion LATEST = new NatsVersion("v2.10.18"); 32 | 33 | /** 34 | * Constant V2_10_18 35 | */ 36 | public static final NatsVersion V2_10_18 = new NatsVersion("v2.10.18"); 37 | 38 | /** 39 | * Constant V2_10_17 40 | */ 41 | public static final NatsVersion V2_10_17 = new NatsVersion("v2.10.17"); 42 | 43 | /** 44 | * Constant V2_10_16 45 | */ 46 | public static final NatsVersion V2_10_16 = new NatsVersion("v2.10.16"); 47 | 48 | /** 49 | * Constant V2_10_14 50 | */ 51 | public static final NatsVersion V2_10_14 = new NatsVersion("v2.10.14"); 52 | 53 | /** 54 | * Constant V2_10_12 55 | */ 56 | public static final NatsVersion V2_10_12 = new NatsVersion("v2.10.12"); 57 | 58 | /** 59 | * Constant V2_10_11 60 | */ 61 | public static final NatsVersion V2_10_11 = new NatsVersion("v2.10.11"); 62 | 63 | /** 64 | * Constant V2_10_10 65 | */ 66 | public static final NatsVersion V2_10_10 = new NatsVersion("v2.10.10"); 67 | 68 | /** 69 | * Constant V2_10_9 70 | */ 71 | public static final NatsVersion V2_10_9 = new NatsVersion("v2.10.9"); 72 | 73 | /** 74 | * Constant V2_10_8 75 | */ 76 | public static final NatsVersion V2_10_8 = new NatsVersion("v2.10.8"); 77 | 78 | /** 79 | * Constant V2_10_7 80 | */ 81 | public static final NatsVersion V2_10_7 = new NatsVersion("v2.10.7"); 82 | 83 | /** 84 | * Constant V2_10_6 85 | */ 86 | public static final NatsVersion V2_10_6 = new NatsVersion("v2.10.6"); 87 | 88 | /** 89 | * Constant V2_10_5 90 | */ 91 | public static final NatsVersion V2_10_5 = new NatsVersion("v2.10.5"); 92 | 93 | /** 94 | * Constant V2_10_4 95 | */ 96 | public static final NatsVersion V2_10_4 = new NatsVersion("v2.10.4"); 97 | 98 | /** 99 | * Constant V2_10_3 100 | */ 101 | public static final NatsVersion V2_10_3 = new NatsVersion("v2.10.3"); 102 | 103 | /** 104 | * Constant V2_10_2 105 | */ 106 | public static final NatsVersion V2_10_2 = new NatsVersion("v2.10.2"); 107 | 108 | /** 109 | * Constant V2_10_1 110 | */ 111 | public static final NatsVersion V2_10_1 = new NatsVersion("v2.10.1"); 112 | 113 | /** 114 | * Constant V2_10_0 115 | */ 116 | public static final NatsVersion V2_10_0 = new NatsVersion("v2.10.0"); 117 | 118 | /** 119 | * Constant V2_9_25 120 | */ 121 | public static final NatsVersion V2_9_25 = new NatsVersion("v2.9.25"); 122 | 123 | /** 124 | * Constant V2_9_24 125 | */ 126 | public static final NatsVersion V2_9_24 = new NatsVersion("v2.9.24"); 127 | 128 | /** 129 | * Constant V2_9_23 130 | */ 131 | public static final NatsVersion V2_9_23 = new NatsVersion("v2.9.23"); 132 | 133 | /** 134 | * Constant V2_9_22 135 | */ 136 | public static final NatsVersion V2_9_22 = new NatsVersion("v2.9.22"); 137 | 138 | /** 139 | * Constant V2_9_21 140 | */ 141 | public static final NatsVersion V2_9_21 = new NatsVersion("v2.9.21"); 142 | 143 | /** 144 | * Constant V2_9_20 145 | */ 146 | public static final NatsVersion V2_9_20 = new NatsVersion("v2.9.20"); 147 | 148 | /** 149 | * Constant V2_9_19 150 | */ 151 | public static final NatsVersion V2_9_19 = new NatsVersion("v2.9.19"); 152 | 153 | /** 154 | * Constant V2_9_18 155 | */ 156 | public static final NatsVersion V2_9_18 = new NatsVersion("v2.9.18"); 157 | 158 | /** 159 | * Constant V2_9_17 160 | */ 161 | public static final NatsVersion V2_9_17 = new NatsVersion("v2.9.17"); 162 | 163 | /** 164 | * Constant V2_9_16 165 | */ 166 | public static final NatsVersion V2_9_16 = new NatsVersion("v2.9.16"); 167 | 168 | /** 169 | * Constant V2_9_15 170 | */ 171 | public static final NatsVersion V2_9_15 = new NatsVersion("v2.9.15"); 172 | 173 | /** 174 | * Constant V2_9_14 175 | */ 176 | public static final NatsVersion V2_9_14 = new NatsVersion("v2.9.14"); 177 | 178 | /** 179 | * Constant V2_9_12 180 | */ 181 | public static final NatsVersion V2_9_12 = new NatsVersion("v2.9.12"); 182 | 183 | /** 184 | * Constant V2_9_11 185 | */ 186 | public static final NatsVersion V2_9_11 = new NatsVersion("v2.9.11"); 187 | 188 | /** 189 | * Constant V2_9_10 190 | */ 191 | public static final NatsVersion V2_9_10 = new NatsVersion("v2.9.10"); 192 | 193 | /** 194 | * Constant V2_9_9 195 | */ 196 | public static final NatsVersion V2_9_9 = new NatsVersion("v2.9.9"); 197 | 198 | /** 199 | * Constant V2_9_8 200 | */ 201 | public static final NatsVersion V2_9_8 = new NatsVersion("v2.9.8"); 202 | 203 | /** 204 | * Constant V2_9_7 205 | */ 206 | public static final NatsVersion V2_9_7 = new NatsVersion("v2.9.7"); 207 | 208 | /** 209 | * Constant V2_9_6 210 | */ 211 | public static final NatsVersion V2_9_6 = new NatsVersion("v2.9.6"); 212 | 213 | /** 214 | * Constant V2_9_5 215 | */ 216 | public static final NatsVersion V2_9_5 = new NatsVersion("v2.9.5"); 217 | 218 | /** 219 | * Constant V2_9_4 220 | */ 221 | public static final NatsVersion V2_9_4 = new NatsVersion("v2.9.4"); 222 | 223 | /** 224 | * Constant V2_9_3 225 | */ 226 | public static final NatsVersion V2_9_3 = new NatsVersion("v2.9.3"); 227 | 228 | /** 229 | * Constant V2_9_2 230 | */ 231 | public static final NatsVersion V2_9_2 = new NatsVersion("v2.9.2"); 232 | 233 | /** 234 | * Constant V2_9_1 235 | */ 236 | public static final NatsVersion V2_9_1 = new NatsVersion("v2.9.1"); 237 | 238 | /** 239 | * Constant V2_9_0 240 | */ 241 | public static final NatsVersion V2_9_0 = new NatsVersion("v2.9.0"); 242 | 243 | /** 244 | * Constant V2_8_4 245 | */ 246 | public static final NatsVersion V2_8_4 = new NatsVersion("v2.8.4"); 247 | 248 | /** 249 | * Constant V2_8_3 250 | */ 251 | public static final NatsVersion V2_8_3 = new NatsVersion("v2.8.3"); 252 | 253 | /** 254 | * Constant V2_8_2 255 | */ 256 | public static final NatsVersion V2_8_2 = new NatsVersion("v2.8.2"); 257 | 258 | /** 259 | * Constant V2_8_1 260 | */ 261 | public static final NatsVersion V2_8_1 = new NatsVersion("v2.8.1"); 262 | 263 | /** 264 | * Constant V2_8_0 265 | */ 266 | public static final NatsVersion V2_8_0 = new NatsVersion("v2.8.0"); 267 | 268 | /** 269 | * Constant V2_7_4 270 | */ 271 | public static final NatsVersion V2_7_4 = new NatsVersion("v2.7.4"); 272 | 273 | /** 274 | * Constant V2_7_3 275 | */ 276 | public static final NatsVersion V2_7_3 = new NatsVersion("v2.7.3"); 277 | 278 | /** 279 | * Constant V2_7_2 280 | */ 281 | public static final NatsVersion V2_7_2 = new NatsVersion("v2.7.2"); 282 | 283 | /** 284 | * Constant V2_7_1 285 | */ 286 | public static final NatsVersion V2_7_1 = new NatsVersion("v2.7.1"); 287 | 288 | /** 289 | * Constant V2_7_0 290 | */ 291 | public static final NatsVersion V2_7_0 = new NatsVersion("v2.7.0"); 292 | 293 | /** 294 | * Constant V2_6_6 295 | */ 296 | public static final NatsVersion V2_6_6 = new NatsVersion("v2.6.6"); 297 | 298 | /** 299 | * Constant V2_6_5 300 | */ 301 | public static final NatsVersion V2_6_5 = new NatsVersion("v2.6.5"); 302 | 303 | /** 304 | * Constant V2_6_4 305 | */ 306 | public static final NatsVersion V2_6_4 = new NatsVersion("v2.6.4"); 307 | 308 | /** 309 | * Constant V2_6_3 310 | */ 311 | public static final NatsVersion V2_6_3 = new NatsVersion("v2.6.3"); 312 | 313 | /** 314 | * Constant V2_6_2 315 | */ 316 | public static final NatsVersion V2_6_2 = new NatsVersion("v2.6.2"); 317 | 318 | /** 319 | * Constant V2_6_1 320 | */ 321 | public static final NatsVersion V2_6_1 = new NatsVersion("v2.6.1"); 322 | 323 | /** 324 | * Constant V2_6_0 325 | */ 326 | public static final NatsVersion V2_6_0 = new NatsVersion("v2.6.0"); 327 | 328 | /** 329 | * Constant V2_5_0 330 | */ 331 | public static final NatsVersion V2_5_0 = new NatsVersion("v2.5.0"); 332 | 333 | /** 334 | * Constant V2_4_0 335 | */ 336 | public static final NatsVersion V2_4_0 = new NatsVersion("v2.4.0"); 337 | 338 | /** 339 | * Constant V2_3_4 340 | */ 341 | public static final NatsVersion V2_3_4 = new NatsVersion("v2.3.4"); 342 | 343 | /** 344 | * Constant V2_3_3 345 | */ 346 | public static final NatsVersion V2_3_3 = new NatsVersion("v2.3.3"); 347 | 348 | /** 349 | * Constant V2_3_2 350 | */ 351 | public static final NatsVersion V2_3_2 = new NatsVersion("v2.3.2"); 352 | 353 | /** 354 | * Constant V2_3_1 355 | */ 356 | public static final NatsVersion V2_3_1 = new NatsVersion("v2.3.1"); 357 | 358 | /** 359 | * Constant V2_3_0 360 | */ 361 | public static final NatsVersion V2_3_0 = new NatsVersion("v2.3.0"); 362 | 363 | /** 364 | * Constant V2_2_6 365 | */ 366 | public static final NatsVersion V2_2_6 = new NatsVersion("v2.2.6"); 367 | 368 | /** 369 | * Constant V2_2_5 370 | */ 371 | public static final NatsVersion V2_2_5 = new NatsVersion("v2.2.5"); 372 | 373 | /** 374 | * Constant V2_2_4 375 | */ 376 | public static final NatsVersion V2_2_4 = new NatsVersion("v2.2.4"); 377 | 378 | /** 379 | * Constant V2_2_3 380 | */ 381 | public static final NatsVersion V2_2_3 = new NatsVersion("v2.2.3"); 382 | 383 | /** 384 | * Constant V2_2_2 385 | */ 386 | public static final NatsVersion V2_2_2 = new NatsVersion("v2.2.2"); 387 | 388 | /** 389 | * Constant V2_2_1 390 | */ 391 | public static final NatsVersion V2_2_1 = new NatsVersion("v2.2.1"); 392 | 393 | /** 394 | * Constant V2_2_0 395 | */ 396 | public static final NatsVersion V2_2_0 = new NatsVersion("v2.2.0"); 397 | 398 | /** 399 | * Constant V2_1_9 400 | */ 401 | public static final NatsVersion V2_1_9 = new NatsVersion("v2.1.9"); 402 | 403 | /** 404 | * Constant V2_1_8 405 | */ 406 | public static final NatsVersion V2_1_8 = new NatsVersion("v2.1.8"); 407 | 408 | /** 409 | * Constant V2_1_7 410 | */ 411 | public static final NatsVersion V2_1_7 = new NatsVersion("v2.1.7"); 412 | 413 | /** 414 | * Constant V2_1_6 415 | */ 416 | public static final NatsVersion V2_1_6 = new NatsVersion("v2.1.6"); 417 | 418 | /** 419 | * Constant V2_1_4 420 | */ 421 | public static final NatsVersion V2_1_4 = new NatsVersion("v2.1.4"); 422 | 423 | /** 424 | * Constant V2_1_2 425 | */ 426 | public static final NatsVersion V2_1_2 = new NatsVersion("v2.1.2"); 427 | /** 428 | * Constant V2_1_0 429 | */ 430 | public static final NatsVersion V2_1_0 = new NatsVersion("v2.1.0"); 431 | /** 432 | * Constant V2_0_4 433 | */ 434 | public static final NatsVersion V2_0_4 = new NatsVersion("v2.0.4"); 435 | /** 436 | * Constant V2_0_2 437 | */ 438 | public static final NatsVersion V2_0_2 = new NatsVersion("v2.0.2"); 439 | /** 440 | * Constant V2_0_0 441 | */ 442 | public static final NatsVersion V2_0_0 = new NatsVersion("v2.0.0"); 443 | 444 | 445 | private final String version; 446 | 447 | /** 448 | *

Constructor for NatsVersion.

449 | * 450 | * @param version a {@link java.lang.String} object. 451 | */ 452 | public NatsVersion(String version) { 453 | this.version = version; 454 | } 455 | 456 | /** 457 | * {@inheritDoc} 458 | */ 459 | @Override 460 | public String asInDownloadPath() { 461 | return version; 462 | } 463 | 464 | /** 465 | * {@inheritDoc} 466 | */ 467 | @Override 468 | public String toString() { 469 | return version; 470 | } 471 | } 472 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/RunningNatsProcess.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 17 | 18 | import de.flapdoodle.embed.process.config.SupportConfig; 19 | import de.flapdoodle.embed.process.io.*; 20 | import de.flapdoodle.embed.process.runtime.ProcessControl; 21 | import de.flapdoodle.embed.process.runtime.Processes; 22 | import de.flapdoodle.embed.process.types.RunningProcessFactory; 23 | import de.flapdoodle.embed.process.types.RunningProcessImpl; 24 | import de.flapdoodle.os.Platform; 25 | import de.flapdoodle.types.Try; 26 | import org.slf4j.Logger; 27 | import org.slf4j.LoggerFactory; 28 | 29 | import java.nio.file.Path; 30 | import java.util.Arrays; 31 | import java.util.Collections; 32 | import java.util.List; 33 | import java.util.regex.Matcher; 34 | import java.util.regex.Pattern; 35 | 36 | /** 37 | *

Abstract RunningNatsProcess class.

38 | * 39 | * @author madanpokharel 40 | * @version $Id: $Id 41 | */ 42 | public abstract class RunningNatsProcess extends RunningProcessImpl { 43 | 44 | private static Logger LOGGER = LoggerFactory.getLogger(RunningNatsProcess.class); 45 | 46 | private final String commandName; 47 | private final SupportConfig supportConfig; 48 | private final Platform platform; 49 | private final StreamProcessor commandOutput; 50 | private final int natsProcessId; 51 | private boolean shutDownCommandAlreadyExecuted = false; 52 | 53 | /** 54 | *

Constructor for RunningNatsProcess.

55 | * 56 | * @param commandName a {@link java.lang.String} object 57 | * @param process a {@link de.flapdoodle.embed.process.runtime.ProcessControl} object 58 | * @param pidFile a {@link java.nio.file.Path} object 59 | * @param onStop a {@link java.lang.Runnable} object 60 | * @param supportConfig a {@link de.flapdoodle.embed.process.config.SupportConfig} object 61 | * @param platform a {@link de.flapdoodle.os.Platform} object 62 | * @param commandOutput a {@link de.flapdoodle.embed.process.io.StreamProcessor} object 63 | * @param natsProcessId a int 64 | * @param serverType a {@link np.com.madanpokharel.embed.nats.ServerType} object 65 | */ 66 | protected RunningNatsProcess( 67 | String commandName, 68 | ProcessControl process, 69 | Path pidFile, 70 | Runnable onStop, 71 | SupportConfig supportConfig, 72 | Platform platform, 73 | StreamProcessor commandOutput, 74 | int natsProcessId, 75 | ServerType serverType 76 | ) { 77 | super(process, pidFile, 5000, onStop); 78 | this.commandName = commandName; 79 | this.supportConfig = supportConfig; 80 | this.platform = platform; 81 | this.commandOutput = commandOutput; 82 | this.natsProcessId = natsProcessId; 83 | } 84 | 85 | /** {@inheritDoc} */ 86 | @Override 87 | public int stop() { 88 | try { 89 | stopInternal(); 90 | } finally { 91 | return super.stop(); 92 | } 93 | } 94 | 95 | // @Override 96 | private void stopInternal() { 97 | if (isAlive()) { 98 | if (!sendKillToProcess()) { 99 | LOGGER.warn("could not stop " + commandName + ", try next"); 100 | if (!sendTermToProcess()) { 101 | LOGGER.warn("could not stop " + commandName + ", try next"); 102 | if (!tryKillToProcess()) { 103 | LOGGER.warn("could not stop " + commandName + " the second time, try one last thing"); 104 | } 105 | } 106 | } 107 | 108 | } 109 | } 110 | 111 | private long getProcessId() { 112 | return natsProcessId; 113 | } 114 | 115 | /** 116 | *

sendKillToProcess.

117 | * 118 | * @return a boolean 119 | */ 120 | protected boolean sendKillToProcess() { 121 | return getProcessId() > 0 && Processes.killProcess(supportConfig, platform, 122 | StreamToLineProcessor.wrap(commandOutput), getProcessId()); 123 | } 124 | 125 | /** 126 | *

sendTermToProcess.

127 | * 128 | * @return a boolean 129 | */ 130 | protected boolean sendTermToProcess() { 131 | return getProcessId() > 0 && Processes.termProcess(supportConfig, platform, 132 | StreamToLineProcessor.wrap(commandOutput), getProcessId()); 133 | } 134 | 135 | /** 136 | *

tryKillToProcess.

137 | * 138 | * @return a boolean 139 | */ 140 | protected boolean tryKillToProcess() { 141 | return getProcessId() > 0 && Processes.tryKillProcess(supportConfig, platform, 142 | StreamToLineProcessor.wrap(commandOutput), getProcessId()); 143 | } 144 | 145 | interface InstanceFactory { 146 | T create(ProcessControl process, Path pidFile, Runnable closeAllOutputs, SupportConfig supportConfig, Platform platform, StreamProcessor commands, int pid, ServerType serverType); 147 | } 148 | 149 | static int getNatsProcessId(String output) { 150 | Pattern pattern = Pattern.compile("\\[\\d+]"); 151 | Matcher matcher = pattern.matcher(output); 152 | if (matcher.find()) { 153 | String value = matcher.group(0); 154 | return Integer.parseInt(value.replace("[", "").replace("]", "")); 155 | } 156 | return -1; 157 | } 158 | 159 | static RunningProcessFactory factory(InstanceFactory instanceFactory, SupportConfig supportConfig, Platform platform, ServerType serverType) { 160 | return (process, processOutput, pidFile, timeout) -> { 161 | 162 | SuccessMessageLineListener logWatch = SuccessMessageLineListener.of(successMessage(serverType), knownFailureMessages(), "error"); 163 | ReaderProcessor output = Processors.connect(process.getReader(), new ListeningStreamProcessor(StreamToLineProcessor.wrap(processOutput.output()), logWatch::inspect)); 164 | ReaderProcessor error = Processors.connect(process.getError(), new ListeningStreamProcessor(StreamToLineProcessor.wrap(processOutput.error()), logWatch::inspect)); 165 | Runnable closeAllOutputs = () -> { 166 | ReaderProcessor.abortAll(output, error); 167 | }; 168 | 169 | logWatch.waitForResult(5000); 170 | if (logWatch.successMessageFound()) { 171 | int pid = getNatsProcessId(logWatch.allLines()); 172 | return instanceFactory.create(process, pidFile, closeAllOutputs, supportConfig, platform, processOutput.commands(), pid, serverType); 173 | 174 | } else { 175 | String failureFound = logWatch.errorMessage().isPresent() 176 | ? logWatch.errorMessage().get() : logWatch.allLines(); 177 | 178 | return Try.supplier(() -> { 179 | throw new RuntimeException("Could not start process: " + failureFound); 180 | }) 181 | .andFinally(() -> { 182 | process.stop(timeout); 183 | }) 184 | .andFinally(closeAllOutputs) 185 | .get(); 186 | } 187 | }; 188 | } 189 | 190 | // VisibleForTesting 191 | 192 | private static List successMessage(ServerType serverType) { 193 | if (serverType == ServerType.NATS) { 194 | return Collections.singletonList("Server is ready"); 195 | } 196 | 197 | return Arrays.asList( 198 | "Streaming Server is ready", 199 | "----------------------------------" 200 | ); 201 | 202 | 203 | } 204 | 205 | private static List knownFailureMessages() { 206 | return Arrays.asList( 207 | "(?Error listening on port)", 208 | "(?flag provided but not defined)", 209 | "(?Error parsing command line:.*)", 210 | "(?address already in use)" 211 | ); 212 | } 213 | 214 | } 215 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/ServerType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 18 | 19 | /** 20 | *

ServerType class.

21 | * 22 | * @author Madan Pokharel 23 | * @version 1.0.0 24 | */ 25 | public enum ServerType { 26 | NATS("nats-server"), 27 | NATS_STREAMING("nats-streaming-server"); 28 | 29 | private final String serverName; 30 | 31 | /** 32 | *

Constructor for ServerType.

33 | * 34 | * @param serverName a {@link java.lang.String} object. 35 | */ 36 | ServerType(String serverName) { 37 | this.serverName = serverName; 38 | } 39 | 40 | /** 41 | *

Getter for the field serverName.

42 | * 43 | * @return a {@link java.lang.String} object. 44 | */ 45 | public String getServerName() { 46 | return serverName; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/np/com/madanpokharel/embed/nats/EmbeddedNatsStreamingTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 18 | 19 | import io.nats.streaming.*; 20 | import org.junit.Test; 21 | 22 | import java.util.UUID; 23 | import java.util.concurrent.CountDownLatch; 24 | import java.util.concurrent.TimeUnit; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class EmbeddedNatsStreamingTest { 29 | 30 | @Test 31 | public void testNatsStreamingWithServerConfig() throws Exception { 32 | int port = 4566; 33 | EmbeddedNatsConfig config = new EmbeddedNatsConfig.Builder() 34 | .withNatsServerConfig( 35 | new NatsServerConfig.Builder() 36 | .withServerType(ServerType.NATS_STREAMING) 37 | .withPort(port) 38 | .withNatsStreamingVersion(NatsStreamingVersion.LATEST) 39 | .build() 40 | ) 41 | .build(); 42 | EmbeddedNatsServer natsStreamingServer = new EmbeddedNatsServer(config); 43 | natsStreamingServer.startServer(); 44 | 45 | assertTrue(natsStreamingServer.isServerRunning()); 46 | assertEquals(port, natsStreamingServer.getRunningPort()); 47 | 48 | natsStreamingServer.stopServer(); 49 | 50 | assertFalse(natsStreamingServer.isServerRunning()); 51 | } 52 | 53 | @Test 54 | public void testNatsStreamingWithDefaultConfig() throws Exception { 55 | EmbeddedNatsConfig config = EmbeddedNatsConfig.defaultNatsStreamingConfig(); 56 | EmbeddedNatsServer nastStreamingServer = new EmbeddedNatsServer(config); 57 | nastStreamingServer.startServer(); 58 | 59 | assertTrue(nastStreamingServer.isServerRunning()); 60 | 61 | nastStreamingServer.stopServer(); 62 | 63 | assertFalse(nastStreamingServer.isServerRunning()); 64 | } 65 | 66 | @Test 67 | public void testNatsStreamingPublishSubscribe() throws Exception { 68 | 69 | String clusterId = "my-cluster" + UUID.randomUUID(); 70 | EmbeddedNatsConfig config = new EmbeddedNatsConfig.Builder() 71 | .withNatsServerConfig( 72 | new NatsServerConfig.Builder() 73 | .withServerType(ServerType.NATS_STREAMING) 74 | .withClusterId(clusterId) 75 | .withRandomPort() 76 | .withNatsStreamingVersion(NatsStreamingVersion.V0_16_2) 77 | .build() 78 | ) 79 | .build(); 80 | EmbeddedNatsServer natsStreamingServer = new EmbeddedNatsServer(config); 81 | natsStreamingServer.startServer(); 82 | 83 | Options options = new Options.Builder() 84 | .natsUrl(natsStreamingServer.getNatsUrl()) 85 | .clusterId(clusterId) 86 | .clientId(UUID.randomUUID().toString()) 87 | .build(); 88 | 89 | StreamingConnectionFactory cf = new StreamingConnectionFactory(options); 90 | StreamingConnection sc = cf.createConnection(); 91 | 92 | String publishedMessage = "Hello World"; 93 | sc.publish("foo", publishedMessage.getBytes()); 94 | 95 | final CountDownLatch doneSignal = new CountDownLatch(1); 96 | 97 | final String[] msg = {""}; 98 | 99 | Subscription sub = sc.subscribe("foo", message -> { 100 | msg[0] = new String(message.getData()); 101 | doneSignal.countDown(); 102 | }, new SubscriptionOptions.Builder().deliverAllAvailable().build()); 103 | 104 | doneSignal.await(1, TimeUnit.SECONDS); 105 | 106 | assertEquals(publishedMessage, msg[0]); 107 | sub.unsubscribe(); 108 | sc.close(); 109 | 110 | natsStreamingServer.stopServer(); 111 | 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/test/java/np/com/madanpokharel/embed/nats/EmbeddedNatsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 18 | 19 | import io.nats.client.Connection; 20 | import io.nats.client.Message; 21 | import io.nats.client.Nats; 22 | import io.nats.client.Subscription; 23 | import org.junit.Test; 24 | 25 | import java.nio.charset.StandardCharsets; 26 | import java.time.Duration; 27 | 28 | import static org.junit.Assert.*; 29 | 30 | public class EmbeddedNatsTest { 31 | 32 | @Test 33 | public void testNatsWithServerConfig() throws Exception { 34 | int port = 4566; 35 | EmbeddedNatsConfig config = new EmbeddedNatsConfig.Builder() 36 | .withNatsServerConfig( 37 | new NatsServerConfig.Builder() 38 | .withPort(port) 39 | .withEnableJetStream() 40 | .withNatsVersion(NatsVersion.LATEST) 41 | .build() 42 | ) 43 | .build(); 44 | EmbeddedNatsServer natsServer = new EmbeddedNatsServer(config); 45 | natsServer.startServer(); 46 | 47 | assertTrue(natsServer.isServerRunning()); 48 | assertEquals(port, natsServer.getRunningPort()); 49 | 50 | natsServer.stopServer(); 51 | 52 | assertFalse(natsServer.isServerRunning()); 53 | } 54 | 55 | @Test 56 | public void testNatsWithDefaultConfig() throws Exception { 57 | EmbeddedNatsConfig config = EmbeddedNatsConfig.defaultNatsServerConfig(); 58 | EmbeddedNatsServer natsServer = new EmbeddedNatsServer(config); 59 | natsServer.startServer(); 60 | 61 | assertTrue(natsServer.isServerRunning()); 62 | 63 | natsServer.stopServer(); 64 | 65 | assertFalse(natsServer.isServerRunning()); 66 | } 67 | 68 | @Test 69 | public void testNatsPublishSubscribe() throws Exception { 70 | 71 | EmbeddedNatsConfig config = EmbeddedNatsConfig.defaultNatsServerConfig(); 72 | EmbeddedNatsServer natsServer = new EmbeddedNatsServer(config); 73 | natsServer.startServer(); 74 | 75 | Connection nc = Nats.connect(natsServer.getNatsUrl()); 76 | 77 | String subject = "mySubject"; 78 | Subscription sub = nc.subscribe(subject); 79 | 80 | String publishedMessage = "hello world"; 81 | nc.publish(subject, publishedMessage.getBytes(StandardCharsets.UTF_8)); 82 | 83 | Message msg = sub.nextMessage(Duration.ofMillis(500)); 84 | 85 | String response = new String(msg.getData(), StandardCharsets.UTF_8); 86 | 87 | assertEquals(publishedMessage, response); 88 | 89 | natsServer.stopServer(); 90 | 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | --------------------------------------------------------------------------------