Giter Site home page Giter Site logo

madansp / nats-embedded Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 6.0 70 KB

Embedded Nats and Nats Streaming(https://nats.io) server for integration testing

License: Apache License 2.0

Java 100.00%
nats-server nats-streaming integration-testing embedded

nats-embedded's Introduction

GitHub code size in bytes License Maven Central

Embedded Nats and Streaming Sever

Embedded Nats and Nats Streaming server for integration tests. Tested with Windows, Mac OSX and Linux

Usage

Add the following dependency to project
For Maven:

<dependency>
   <groupId>np.com.madanpokharel.embed</groupId>
   <artifactId>nats-embedded</artifactId>
   <version>2.1.2</version>
   <scope>test</scope>
 </dependency>

For Gradle(Groovy Style):

testCompile group: 'np.com.madanpokharel.embed', name: 'nats-embedded', version: '2.1.2'

For Gradle

implementation("np.com.madanpokharel.embed:nats-embedded:2.1.2")

For Bazel

maven_jar(
    name = "nats-embedded",
    artifact = "np.com.madanpokharel.embed:nats-embedded:2.1.2",
    sha1 = "00f752b7b1c1758eb2f1aa35cf2352ba67d91710",
)

Example

Start nats server with defined port 7656.

 EmbeddedNatsConfig config = new EmbeddedNatsConfig.Builder()
                .withNatsServerConfig(
                        new NatsServerConfig.Builder()
                                .withServerType(ServerType.NATS) //Can be omitted, default is Nats
                                .withPort(7656)
                                .withNatsVersion(NatsVersion.LATEST)
                                .build()
                )
                .build();
        EmbeddedNatsServer natsServer = new EmbeddedNatsServer(config);
        //start the server
        natsServer.startServer();

//get running port
System.out.pritnln(natsServer.getRunningPort())

//stop the server
natsServer.stopServer();

Start with default settings. This will download latest version and start server on random port.

EmbeddedNatsConfig config =  EmbeddedNatsConfig.defaultNatsServerConfig();
EmbeddedNatsServer natsServer = new EmbeddedNatsServer(config);
natsServer.startServer();

//get running port
System.out.println(natsServer.getRunningPort())

natsServer.stopServer();

Start with custom config params

EmbeddedNatsConfig config = new EmbeddedNatsConfig.Builder()
                .withNatsServerConfig(
                        new NatsServerConfig.Builder()
                                .withServerType(ServerType.NATS_STREAMING)
                                .withClusterId(clusterId)
                                .withRandomPort()
                                .withConfigParam("--store","FILE") //custom config parameter
                                .withConfigParam("--dir","target/nats") //custom config parameter
                                .withNatsStreamingVersion(NatsStreamingVersion.LATEST)
                                .build()
                )
                .build();
        EmbeddedNatsServer natsStreamingServer = new EmbeddedNatsServer(config);
        natsStreamingServer.startServer();

Customizing the download location and extraction path

 EmbeddedNatsConfig config = new EmbeddedNatsConfig.Builder()
                .withNatsServerConfig(
                        new NatsServerConfig.Builder()
                                .withServerType(ServerType.NATS)
                                .withPort(port)
                                .withNatsVersion(NatsVersion.LATEST)
                                .build()
                )
                .withArtifactStorePath(".custom-download-dir") //custom download location
                .withExtractDirectory(".custom-extract-dir") // custom location for extraction of zip file
                .build();

Example of Nats server with full configurations

EmbeddedNatsConfig config = new EmbeddedNatsConfig.Builder()
                .withNatsServerConfig( //configuration for nats server configuration
                        new NatsServerConfig.Builder()
                                .withServerType(ServerType.NATS) // server type either nats or nats streaming
                                .withPort(2345) // port to run. can withRandomPort() if want to run on random port
                                .withNatsVersion(NatsVersion.V2_1_0) // version to use
                                .withHost("127.0.0.1") // host ip
                                .withConfigParam("--store","FILE") // single config parameter 
                                .withConfigParams(Collections.singletonMap("--dir","target/nats")) // map of config parameters
                                .build()
                )
                .withArtifactStorePath(".custom-download-dir") // location for saving downloaded file
                .withExtractDirectory(".custom-extract-dir") // location for extracting downloaded zip file
                .build();

Example of Nats Streaming server with full configurations

EmbeddedNatsConfig config = new EmbeddedNatsConfig.Builder()
                .withNatsServerConfig( //configuration for nats server configuration
                        new NatsServerConfig.Builder()
                                .withServerType(ServerType.NATS_STREAMING) // server type either nats or nats streaming
                                .withPort(3445) // port to run. can withRandomPort() if want to run on random port
                                .withNatsStreamingVersion(NatsStreamingVersion.V0_16_2)  // version to use
                                .withClusterId("clusterId") //cluster Id
                                .withHost("127.0.0.1") // host ip
                                .withConfigParam("--store","FILE") // single config parameter
                                .withConfigParams(Collections.singletonMap("--dir","target/nats")) // map of config parameters
                                .build()
                )
                .withArtifactStorePath(".custom-download-dir") // location for saving downloaded file
                .withExtractDirectory(".custom-extract-dir") // location for extracting downloaded zip file
                .build();

Using custom nats configuration file

new NatsServerConfig.Builder()
        .withConfigurationFile("custom-config-file.conf")
        .build()

Enabling JetStream

 new NatsServerConfig.Builder()
        .withEnableJetStream() //without store location
        .build() 
        
new NatsServerConfig.Builder()
        .withEnableJetStream("target") //with store location
        .build()

Dependency

This library using component from flapdoodle-oss

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.