Jms Adapter
=============

Introduction
------------
The JMS adapter can be used to connect a Diffusion server (or server cluster) to any JMS compliant broker.
It can consume messages from the JMS broker and publish them to Diffusion, and vice versa.
It supports any traditional JMS Broker such as ActiveMQ, IBM MQ etc.
It uses JNDI to create connections with JMS brokers and to send and receive messages to/from them.

The adapter can be viewed and managed in the Diffusion management console of the connected server or in any other server in the same cluster.
Once started, the adapter will be visible in the Diffusion console, in the 'Network' section, as a gateway client of type 'JMS' with the id specified in the configuration.

The adapter can be configured to connect to multiple types of JMS brokers.
It has two major functions:
1. Publishing to Diffusion topics - Consumes from JMS destinations and publishes to Diffusion
2. Subscribing to Diffusion topics - Subscribes to topics in Diffusion and sends the updates to JMS providers

The configuration for these features can be set via a JSON configuration file, which will be used during adapter startup.
The configuration can also be added or updated during runtime via Diffusion console, as a service.

Services
--------
A 'service' is a concept introduced from 6.7 version of JMS adapter, which represents a specific function that the adapter supports.
A service can be of specific type which is identified by a unique name.
Each service will have its type, name, description and configuration.
The adapter's overall configuration is determined by the configuration of individual services.
The services can be added/updated/removed via the Diffusion console, after the adapter is started.
If the adapter is started with a complete configuration file, these configurations will be used to create services,
which will be visible in the console.

The JMS adapter supports three types of service which are required to set up publish-subscribe interactions between JMS providers and Diffusion.

JMS Provider Registration service
---------------------------------
This service enables you to register a JMS provider to the adapter for use by other services.
There can be multiple instances of this service added to the adapter.
Adding this service will start a connection with the specified JMS provider.
The JMS provider can then be used by the publisher or subscriber service to publish or subscribe to JMS destinations.

This service does not support any operations.

Diffusion Publisher Service
---------------------------
A Diffusion publisher service is used to subscribe to a list of JMS destinations and publish the updates to specific Diffusion topics.
At least one `jmsProviderRegister` service must be defined before adding this service.
There can be multiple instances of this service added to the adapter to publish to different sets of Diffusion topics with different configuration options.

The publisher service supports the following two operations, which can be invoked via the Diffusion console:
1. Pause - closes JMS message consumers for all JMS subscriptions; closes the JMS session used for the service.
2. Resume - re-creates the JMS session for the service and re-creates the JMS message consumers for all JMS subscriptions.

Diffusion Subscriber Service
----------------------------
A Diffusion subscriber service is used to subscribe to a list of Diffusion topic selectors and publish the updates to specific JMS destination.
At least one `jmsProviderRegister` service must be defined before adding this service.
There can be multiple instances of this service added to the adapter to subscribe to different sets of Diffusion topics with different configuration options.

The subscriber service supports the following two operations, which can be invoked via the Diffusion console:
1. Pause - unsubscribe from all Diffusion topic selectors.
2. Resume - re-subscribe to all Diffusion topic selectors for the service.

Operations
----------
Operations are new to Diffusion 6.7. Operations can be used to interact with the adapter via the Diffusion management console.
The JMS adapter supports three operations, which can be invoked via the Diffusion console.

1. Pause - pause all Diffusion publisher and subscriber services.
2. Resume - resume all Diffusion publisher and subscriber services.
3. Shut down - terminate the adapter by shutting down all resources.

Configurations
--------------
The adapter can be started with only bootstrap configuration, with only a configuration file or with a combination of both.
Bootstrap configuration refers to the details required to connect to a Diffusion server, which are server URL, principal and password.
These bootstrap configurations can be passed as system properties (set using a command line parameter) or environment variables, which will be used by default,
if they are not set in the configuration file.

The allowed system/env properties are:
```
diffusion.gateway.server.url
diffusion.gateway.principal
diffusion.gateway.password
gateway.client.id
```
When the adapter is started, it registers itself with the server using its id (defined in the configuration file) and configuration.
The configuration will be empty if a configuration file is not passed as an argument when starting the adapter.
The server persists this information in its local persistent file.
If a service is added/updated/removed, the configuration of the adapter changes during runtime and it is updated in the server for the adapter id.
Hence, if the adapter is shut down and started again, it's not required to pass the configuration file, as it would get the persisted configuration from the server.

To override the configuration persisted in server, it can be done by passing the configuration file as an argument.
The passed configuration will replace the configuration persisted in the server and will be used to setup expected services.

See the next section for examples.

Running the JMS adapter
=========================
This directory contains the JMS adapter installation and the necessary resources to run the adapter.
The adapter does not package the JMS client jar and SLF4J binding jar which needs to be added into the adapter
installation path and included in the classpath, as required, when running the adapter.

Prerequisites
=============
1. A running JMS broker
2. A running Diffusion server (or server cluster)
3. JMS provider's client jar

Common Tasks
============

Configuration
-------------
Edit the configuration.json file, provided in this directory and add necessary details.
The JSON schema file configSchema.json can be used to edit and validate the configuration file.

Set up the adapter
----------------
Add necessary JMS client jar in the adapter installation path.
Add desired logging framework jar and Slf4j binding jar in the adapter installation path, if its not included in the JMS client jar.
The logger needs to be configured as per the logging framework that you wish to use.

Start the adapter
----------------
The adapter requires a Java installation. The Java requirements are the same as for the Diffusion server.

Run the jar provided as jmsadapter-6.11.8.jar, by including the JMS client and SLF4j jars in the classpath.
There are two ways to pass the configuration to the adapter:
1. By passing the configuration file as an argument:
 e.g: java -cp activemq-all-5.16.0.jar:jmsadapter-6.11.8.jar com.pushtechnology.diffusion.adapters.jms.AdapterRunner configuration.json`
2.By only passing bootstrap configurations as system properties:
 e.g: java -Dgateway.client.id=testJmsAdapter -Ddiffusion.gateway.server.url=ws://localhost:8080 -Ddiffusion.gateway.principal=admin -Ddiffusion.gateway.password=password -cp activemq-all-5.16.0.jar:jmsadapter-6.11.8.jar com.pushtechnology.diffusion.adapters.jms.AdapterRunner`

Alternatively, to start the adapter on Linux or macOS, update and run the `jms_adapter.sh` script.

    ./jms_adapter.sh

On Windows, use the `jms_adapter.bat` script instead.

Set up a secure connection with Diffusion
-----------------------------------------

1. Import the Certificate Authority certificate which is used to sign the server's certificate to the local truststore:
    `keytool -keystore local.client.truststore.jks -alias <alias> -import -file <ca-cert file name>  -storepass <password> -keypass <password> -noprompt`
2. Add the following system properties to the java run command in "jms_adapter.sh"
    `-Djavax.net.ssl.trustStore=./local.client.trustStore.jks -Djavax.net.ssl.trustStorePassword=<password>`
3. Update the URL of the Diffusion server in configuration.json to use "wss://"

Monitoring
----------
The adapter can be viewed and monitored in Diffusion management console of the connected server or in any other server in same cluster.
