.. _client_jms: |cjms| for |cp| =============== Java Message Service (JMS) is a widely used messaging API that is included as part of the Java Platform, Enterprise Edition. |cjms-full| (``kafka-jms-client``) is an implementation of the `JMS 1.1 provider interface `_ that allows |ak-tm| or |cp| to be used as a JMS message broker. ``[kafka-jms-client] <--- kafka protocol ---> [kafka broker]`` Applications written using the Confluent ``kafka-jms-client`` can exchange messages with other application written using ``kafka-jms-client`` as well as any application that produces or consumes messages using any other |ak| client library. The purpose of this client is to allow existing applications written to the JMS API to be able to publish and subscribe to |ak| as a replacement for any other JMS broker. If you are interested in integrating |ak| with an existing JMS broker (that is, without replacing it) then you are encouraged to look at the :ref:`Kafka Connect API ` or any pre-built |ak| Source or Sink Connectors for JMS. If you are interested in writing new Java applications then you are encouraged to use the Java |ak| Producer/Consumer APIs as they provide advanced features not available when using the ``kafka-jms-client``. JMS 1.1 Compatibility --------------------- Where possible, the |ak| |cjms| is a complete implementation of the JMS 1.1 specification. However there are some JMS concepts that either do not map 1:1 to |ak|, or simply do not make sense at all in |ak| (such as non-persistent messages). JMS brokers typically provide distinct `Topic `_ and `Queue `_ functionality that reflects the topic and queue functionality in the JMS Specification. By contrast, |ak| provides only one abstraction - partitioned topics - which are actually distributed commit logs and behave differently than the traditional messaging system notion of a topic. In practice, |ak| topics can mimic the behavior of either topics or queues in the traditional messaging system sense. Additional notes on JMS 1.1 compatibility are outlined below. Features ^^^^^^^^ Both JMS messaging models are supported: * Publish/Subscribe (Topics) * Point-to-Point (Queues) All JMS message types are supported including: * ``TextMessage`` * ``BytesMessage`` * ``MapMessage`` * ``StreamMessage`` * ``ObjectMessage`` Treatment of JMS header values: * ``JMSDestination`` - Set (overwritten) to on send and available in message at the destination. * ``JMSDeliveryMode`` - Set (overwritten) to DEAULT_DELIVERY_MODE (PERSISTED) on send. * ``JMSExpiration`` - Ignored. Overwritten with 0 on send. * ``JMSPriority`` - Ignored. Set (overwritten) with DEFAULT_PRIORITY (4) on send. * ``JMSMessageID`` - Set (overwritten) with a unique id for the message on send and available in message at destination. * ``JMSTimestamp`` - Set (overwritten) on message send and transmitted to destination if this has not been disabled using ``MessageProducer.setDisableMessageTimestamp``. * ``JMSCorrelationID`` - Value set by user is transmitted to destination. * ``JMSReplyTo`` - Value set by user is transmitted to destination. * ``JMSType`` - Value set by user is transmitted to destination. * ``JMSRedelivered`` - Ignored. Set (overwritten) to false on send. Only durable topics/queues are supported. Unsupported Features ^^^^^^^^^^^^^^^^^^^^ The following functionality has either limited support or no support: Session ~~~~~~~ ======================================================= ======================================================================================= Method Notes ======================================================= ======================================================================================= createDurableSubscriber(Topic, String) Durable Subscribers are not supported. createDurableSubscriber(Topic, String, String, boolean) Durable Subscribers are not supported. getMessageListener() Session based MessageListeners are not supported. setMessageListener(MessageListener) Session based MessageListeners are not supported. rollback() Transactions are not supported by |ak|. recover() Transactions are not supported by |ak| commit() Transactions are not supported by |ak|. createBrowser(Queue, String) QueueBrowsers with message selectors are not supported. unsubscribe(String) Durable Subscribers are not supported. createConsumer(Destination, String) Message selectors are not supported. An exception will be thrown if one is set. createConsumer(Destination, String, boolean) Message selectors are not supported. An exception will be thrown if one is set. createTemporaryTopic() |ak| does not have a concept of a temporary topic. getTransacted() Transactions are not supported. This will always return false. createTemporaryQueue() |ak| does not have a concept of a temporary topic. ======================================================= ======================================================================================= ConnectionFactory ~~~~~~~~~~~~~~~~~ ================================= ========================================================================================================= Method Notes ================================= ========================================================================================================= createConnection(String, String) Not supported. Authentication is only supported via |ak| configuration properties. ================================= ========================================================================================================= Connection ~~~~~~~~~~ ======================================= ================================================================================================================ Method Notes ======================================= ================================================================================================================ setClientID(String) Setting the client id is only supported by using `client.id` in the settings passed to KafkaConnectionFactory. ======================================= ================================================================================================================ .. _client_jms_installation: |cjms| Installation ------------------- Prerequisites See the |cp| :ref:`system-requirements`. Installation ^^^^^^^^^^^^ The |cjms| is a library that you use from within your Java applications. To reference ``kafka-jms-client`` in a Maven-based project, first add the Confluent Maven repository to your ``pom.xml``:: confluent http://packages.confluent.io/maven/ Then add a dependency on the |cjms-full| and the JMS API specification: .. codewithvars:: bash io.confluent kafka-jms-client |release| org.apache.geronimo.specs geronimo-jms_1.1_spec 1.1 If you don't use Maven, you can download the |cjms| JAR file directly by navigating to the following URL. .. codewithvars:: bash http://packages.confluent.io/maven/io/confluent/kafka-jms-client/|release|/kafka-jms-client -|release|.jar If you require a "fat" JAR (one that includes the |cjms| and all of its dependencies), you can make one by following the instructions in :ref:`appendix_1`. Example ^^^^^^^ Usage of ``kafka-jms-client`` is similar to the JMS API. The following example program uses a ``KafkaConnectionFactory`` instance to create JMS compliant ``Connection``, ``Session`` and ``MessageProducer`` objects. The ``MessageProducer`` is then used to send 50 ``TextMessage`` messages to the |ak-tm| topic ``test-queue``, which is acting as a queue. A ``MessageConsumer`` is then created and used to read back these messages. .. codewithvars:: java import java.util.Properties; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import io.confluent.kafka.jms.JMSClientConfig; import io.confluent.kafka.jms.KafkaConnectionFactory; public class App { public static void main(String[] args) throws JMSException { Properties settings = new Properties(); settings.put(JMSClientConfig.CLIENT_ID_CONFIG, "test-client-2"); settings.put(JMSClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); settings.put(JMSClientConfig.ZOOKEEPER_CONNECT_CONF, "localhost:2181"); ConnectionFactory connectionFactory = new KafkaConnectionFactory(settings); Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination testQueue = session.createQueue("test-queue"); MessageProducer producer = session.createProducer(testQueue); for (int i=0; i<50; i++) { TextMessage message = session.createTextMessage(); message.setText("This is a text message"); producer.send(message); } MessageConsumer consumer = session.createConsumer(testQueue); while (true) { TextMessage message = (TextMessage)consumer.receive(); System.out.println(message.getText()); } } } .. _appendix_1: Appendix 1 - Creating a Shaded Fat JAR ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ In some scenarios, it is useful to have a "fat" JAR that bundles the |cjms| together with all of its dependencies in a single file. Confluent does not distribute the |cjms| in this form, but you can build a fat JAR yourself easily enough: 1. Copy the ``pom.xml`` below into an empty directory. 2. Execute the Maven command ``mvn package``. The resulting artifact will be placed in the ``target`` directory along side the ``pom.xml`` file. Note: In addition to bundling dependencies, the provided ``pom.xml`` file *shades* them under the namespace ``confluent.shaded.`` to avoid potential namespace clashes. .. codewithvars:: bash 4.0.0 io.confluent kafka-jms-client-fat |release| confluent http://packages.confluent.io/maven/ io.confluent kafka-jms-client |release| org.apache.maven.plugins maven-shade-plugin 2.4.3 package shade org.I0Itec. confluent.shaded.org.I0Itec. com.yammer.metrics. confluent.shaded.com.yammer.metrics. joptsimple confluent.shaded.joptsimple org.apache.zookeeper. confluent.shaded.org.apache.zookeeper. org.apache.jute. confluent.shaded.org.apache.jute. org.apache.kafka. confluent.shaded.org.apache.kafka. org.apache.log4j. confluent.shaded.org.apache.log4j. com.google.common. confluent.shaded.com.google.common. com.google.thirdparty. confluent.shaded.com.google.thirdparty. com.fasterxml.jackson. confluent.shaded.com.fasterxml.jackson. net.jpountz. confluent.shaded.net.jpountz. org.xerial.snappy. confluent.shaded.org.xerial.snappy. org.jose4j. confluent.shaded.org.jose4j. io.confluent.common. confluent.shaded.io.confluent.common. io.confluent.license. confluent.shaded.io.confluent.license. kafka. confluent.shaded.kafka. scala confluent.shaded.scala. src/main/resources true Suggested Reading ----------------- - :ref:`Installation, Download, and Usage for the JMS Client ` - :ref:`client_jms_developing` - :ref:`JMS Client Overview `