.. _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| as well as 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 it's 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