blob: bf09934e15a416f1acd9b156c319c42439010957 [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en" >
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Using JMS | ActiveMQ Artemis Documentation</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.1.0">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="next" href="./using-core.html" />
<link rel="prev" href="./using-server.html" />
</head>
<body>
<div class="book" data-level="7" data-basepath="." data-revision="Wed Jun 08 2016 13:05:37 GMT+0100 (BST)">
<div class="book-summary">
<div class="book-search">
<input type="text" placeholder="Type to search" class="form-control" />
</div>
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
Introduction
</a>
</li>
<li class="chapter " data-level="1" data-path="notice.html">
<a href="./notice.html">
<i class="fa fa-check"></i>
<b>1.</b>
Legal Notice
</a>
</li>
<li class="chapter " data-level="2" data-path="preface.html">
<a href="./preface.html">
<i class="fa fa-check"></i>
<b>2.</b>
Preface
</a>
</li>
<li class="chapter " data-level="3" data-path="project-info.html">
<a href="./project-info.html">
<i class="fa fa-check"></i>
<b>3.</b>
Project Info
</a>
</li>
<li class="chapter " data-level="4" data-path="messaging-concepts.html">
<a href="./messaging-concepts.html">
<i class="fa fa-check"></i>
<b>4.</b>
Messaging Concepts
</a>
</li>
<li class="chapter " data-level="5" data-path="architecture.html">
<a href="./architecture.html">
<i class="fa fa-check"></i>
<b>5.</b>
Architecture
</a>
</li>
<li class="chapter " data-level="6" data-path="using-server.html">
<a href="./using-server.html">
<i class="fa fa-check"></i>
<b>6.</b>
Using the Server
</a>
</li>
<li class="chapter active" data-level="7" data-path="using-jms.html">
<a href="./using-jms.html">
<i class="fa fa-check"></i>
<b>7.</b>
Using JMS
</a>
</li>
<li class="chapter " data-level="8" data-path="using-core.html">
<a href="./using-core.html">
<i class="fa fa-check"></i>
<b>8.</b>
Using Core
</a>
</li>
<li class="chapter " data-level="9" data-path="jms-core-mapping.html">
<a href="./jms-core-mapping.html">
<i class="fa fa-check"></i>
<b>9.</b>
Mapping JMS Concepts to the Core API
</a>
</li>
<li class="chapter " data-level="10" data-path="client-classpath.html">
<a href="./client-classpath.html">
<i class="fa fa-check"></i>
<b>10.</b>
The Client Classpath
</a>
</li>
<li class="chapter " data-level="11" data-path="examples.html">
<a href="./examples.html">
<i class="fa fa-check"></i>
<b>11.</b>
Examples
</a>
</li>
<li class="chapter " data-level="12" data-path="wildcard-routing.html">
<a href="./wildcard-routing.html">
<i class="fa fa-check"></i>
<b>12.</b>
Routing Messages With Wild Cards
</a>
</li>
<li class="chapter " data-level="13" data-path="wildcard-syntax.html">
<a href="./wildcard-syntax.html">
<i class="fa fa-check"></i>
<b>13.</b>
Understanding the Apache ActiveMQ Artemis Wildcard Syntax
</a>
</li>
<li class="chapter " data-level="14" data-path="filter-expressions.html">
<a href="./filter-expressions.html">
<i class="fa fa-check"></i>
<b>14.</b>
Filter Expressions
</a>
</li>
<li class="chapter " data-level="15" data-path="persistence.html">
<a href="./persistence.html">
<i class="fa fa-check"></i>
<b>15.</b>
Persistence
</a>
</li>
<li class="chapter " data-level="16" data-path="configuring-transports.html">
<a href="./configuring-transports.html">
<i class="fa fa-check"></i>
<b>16.</b>
Configuring Transports
</a>
</li>
<li class="chapter " data-level="17" data-path="connection-ttl.html">
<a href="./connection-ttl.html">
<i class="fa fa-check"></i>
<b>17.</b>
Detecting Dead Connections
</a>
</li>
<li class="chapter " data-level="18" data-path="slow-consumers.html">
<a href="./slow-consumers.html">
<i class="fa fa-check"></i>
<b>18.</b>
Detecting Slow Consumers
</a>
</li>
<li class="chapter " data-level="19" data-path="transaction-config.html">
<a href="./transaction-config.html">
<i class="fa fa-check"></i>
<b>19.</b>
Resource Manager Configuration
</a>
</li>
<li class="chapter " data-level="20" data-path="flow-control.html">
<a href="./flow-control.html">
<i class="fa fa-check"></i>
<b>20.</b>
Flow Control
</a>
</li>
<li class="chapter " data-level="21" data-path="send-guarantees.html">
<a href="./send-guarantees.html">
<i class="fa fa-check"></i>
<b>21.</b>
Guarantees of sends and commits
</a>
</li>
<li class="chapter " data-level="22" data-path="undelivered-messages.html">
<a href="./undelivered-messages.html">
<i class="fa fa-check"></i>
<b>22.</b>
Message Redelivery and Undelivered Messages
</a>
</li>
<li class="chapter " data-level="23" data-path="message-expiry.html">
<a href="./message-expiry.html">
<i class="fa fa-check"></i>
<b>23.</b>
Message Expiry
</a>
</li>
<li class="chapter " data-level="24" data-path="large-messages.html">
<a href="./large-messages.html">
<i class="fa fa-check"></i>
<b>24.</b>
Large Messages
</a>
</li>
<li class="chapter " data-level="25" data-path="paging.html">
<a href="./paging.html">
<i class="fa fa-check"></i>
<b>25.</b>
Paging
</a>
</li>
<li class="chapter " data-level="26" data-path="queue-attributes.html">
<a href="./queue-attributes.html">
<i class="fa fa-check"></i>
<b>26.</b>
Queue Attributes
</a>
</li>
<li class="chapter " data-level="27" data-path="scheduled-messages.html">
<a href="./scheduled-messages.html">
<i class="fa fa-check"></i>
<b>27.</b>
Scheduled Messages
</a>
</li>
<li class="chapter " data-level="28" data-path="last-value-queues.html">
<a href="./last-value-queues.html">
<i class="fa fa-check"></i>
<b>28.</b>
Last-Value Queues
</a>
</li>
<li class="chapter " data-level="29" data-path="message-grouping.html">
<a href="./message-grouping.html">
<i class="fa fa-check"></i>
<b>29.</b>
Message Grouping
</a>
</li>
<li class="chapter " data-level="30" data-path="pre-acknowledge.html">
<a href="./pre-acknowledge.html">
<i class="fa fa-check"></i>
<b>30.</b>
Extra Acknowledge Modes
</a>
</li>
<li class="chapter " data-level="31" data-path="management.html">
<a href="./management.html">
<i class="fa fa-check"></i>
<b>31.</b>
Management
</a>
</li>
<li class="chapter " data-level="32" data-path="security.html">
<a href="./security.html">
<i class="fa fa-check"></i>
<b>32.</b>
Security
</a>
</li>
<li class="chapter " data-level="33" data-path="resource-limits.html">
<a href="./resource-limits.html">
<i class="fa fa-check"></i>
<b>33.</b>
Resource Limits
</a>
</li>
<li class="chapter " data-level="34" data-path="jms-bridge.html">
<a href="./jms-bridge.html">
<i class="fa fa-check"></i>
<b>34.</b>
The JMS Bridge
</a>
</li>
<li class="chapter " data-level="35" data-path="client-reconnection.html">
<a href="./client-reconnection.html">
<i class="fa fa-check"></i>
<b>35.</b>
Client Reconnection and Session Reattachment
</a>
</li>
<li class="chapter " data-level="36" data-path="diverts.html">
<a href="./diverts.html">
<i class="fa fa-check"></i>
<b>36.</b>
Diverting and Splitting Message Flows
</a>
</li>
<li class="chapter " data-level="37" data-path="core-bridges.html">
<a href="./core-bridges.html">
<i class="fa fa-check"></i>
<b>37.</b>
Core Bridges
</a>
</li>
<li class="chapter " data-level="38" data-path="duplicate-detection.html">
<a href="./duplicate-detection.html">
<i class="fa fa-check"></i>
<b>38.</b>
Duplicate Message Detection
</a>
</li>
<li class="chapter " data-level="39" data-path="clusters.html">
<a href="./clusters.html">
<i class="fa fa-check"></i>
<b>39.</b>
Clusters
</a>
</li>
<li class="chapter " data-level="40" data-path="ha.html">
<a href="./ha.html">
<i class="fa fa-check"></i>
<b>40.</b>
High Availability and Failover
</a>
</li>
<li class="chapter " data-level="41" data-path="graceful-shutdown.html">
<a href="./graceful-shutdown.html">
<i class="fa fa-check"></i>
<b>41.</b>
Graceful Server Shutdown
</a>
</li>
<li class="chapter " data-level="42" data-path="libaio.html">
<a href="./libaio.html">
<i class="fa fa-check"></i>
<b>42.</b>
Libaio Native Libraries
</a>
</li>
<li class="chapter " data-level="43" data-path="thread-pooling.html">
<a href="./thread-pooling.html">
<i class="fa fa-check"></i>
<b>43.</b>
Thread management
</a>
</li>
<li class="chapter " data-level="44" data-path="logging.html">
<a href="./logging.html">
<i class="fa fa-check"></i>
<b>44.</b>
Logging
</a>
</li>
<li class="chapter " data-level="45" data-path="rest.html">
<a href="./rest.html">
<i class="fa fa-check"></i>
<b>45.</b>
REST Interface
</a>
</li>
<li class="chapter " data-level="46" data-path="embedding-activemq.html">
<a href="./embedding-activemq.html">
<i class="fa fa-check"></i>
<b>46.</b>
Embedding Apache ActiveMQ Artemis
</a>
</li>
<li class="chapter " data-level="47" data-path="karaf.html">
<a href="./karaf.html">
<i class="fa fa-check"></i>
<b>47.</b>
Apache Karaf
</a>
</li>
<li class="chapter " data-level="48" data-path="spring-integration.html">
<a href="./spring-integration.html">
<i class="fa fa-check"></i>
<b>48.</b>
Spring Integration
</a>
</li>
<li class="chapter " data-level="49" data-path="aerogear-integration.html">
<a href="./aerogear-integration.html">
<i class="fa fa-check"></i>
<b>49.</b>
AeroGear Integration
</a>
</li>
<li class="chapter " data-level="50" data-path="vertx-integration.html">
<a href="./vertx-integration.html">
<i class="fa fa-check"></i>
<b>50.</b>
VertX Integration
</a>
</li>
<li class="chapter " data-level="51" data-path="intercepting-operations.html">
<a href="./intercepting-operations.html">
<i class="fa fa-check"></i>
<b>51.</b>
Intercepting Operations
</a>
</li>
<li class="chapter " data-level="52" data-path="protocols-interoperability.html">
<a href="./protocols-interoperability.html">
<i class="fa fa-check"></i>
<b>52.</b>
Protocols and Interoperability
</a>
</li>
<li class="chapter " data-level="53" data-path="tools.html">
<a href="./tools.html">
<i class="fa fa-check"></i>
<b>53.</b>
Tools
</a>
</li>
<li class="chapter " data-level="54" data-path="maven-plugin.html">
<a href="./maven-plugin.html">
<i class="fa fa-check"></i>
<b>54.</b>
Maven Plugin
</a>
</li>
<li class="chapter " data-level="55" data-path="perf-tuning.html">
<a href="./perf-tuning.html">
<i class="fa fa-check"></i>
<b>55.</b>
Troubleshooting and Performance Tuning
</a>
</li>
<li class="chapter " data-level="56" data-path="configuration-index.html">
<a href="./configuration-index.html">
<i class="fa fa-check"></i>
<b>56.</b>
Configuration Reference
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
<a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
<div id="font-settings-wrapper" class="dropdown pull-left">
<a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
</a>
<div class="dropdown-menu font-settings">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" id="reduce-font-size" class="button size-2">A</button>
<button type="button" id="enlarge-font-size" class="button size-2">A</button>
</div>
<div class="buttons font-family-list">
<button type="button" data-font="0" class="button">Serif</button>
<button type="button" data-font="1" class="button">Sans</button>
</div>
<div class="buttons color-theme-list">
<button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
<button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
<button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
</div>
</div>
</div>
<!-- Actions Right -->
<div class="dropdown pull-right">
<a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
</a>
<div class="dropdown-menu font-settings dropdown-left">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" data-sharing="twitter" class="button">
Share on Twitter
</button>
<button type="button" data-sharing="google-plus" class="button">
Share on Google
</button>
<button type="button" data-sharing="facebook" class="button">
Share on Facebook
</button>
<button type="button" data-sharing="weibo" class="button">
Share on Weibo
</button>
<button type="button" data-sharing="instapaper" class="button">
Share on Instapaper
</button>
</div>
</div>
</div>
<a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
<a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
<a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >ActiveMQ Artemis Documentation</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1">
<div class="page-inner">
<section class="normal" id="section-">
<h1 id="using-jms">Using JMS</h1>
<p>Although Apache ActiveMQ Artemis provides a JMS agnostic messaging API, many users will
be more comfortable using JMS.</p>
<p>JMS is a very popular API standard for messaging, and most messaging
systems provide a JMS API. If you are completely new to JMS we suggest
you follow the <a href="http://docs.oracle.com/javaee/7/tutorial/partmessaging.htm" target="_blank">Oracle JMS tutorial</a> -
a full JMS tutorial is out of scope for this guide.</p>
<p>Apache ActiveMQ Artemis also ships with a wide range of examples, many of which
demonstrate JMS API usage. A good place to start would be to play around
with the simple JMS Queue and Topic example, but we also provide
examples for many other parts of the JMS API. A full description of the
examples is available in <a href="examples.html">Examples</a>.</p>
<p>In this section we&apos;ll go through the main steps in configuring the
server for JMS and creating a simple JMS program. We&apos;ll also show how to
configure and use JNDI, and also how to use JMS with Apache ActiveMQ Artemis without
using any JNDI.</p>
<h1 id="a-simple-ordering-system">A simple ordering system</h1>
<p>For this chapter we&apos;re going to use a very simple ordering system as our
example. It is a somewhat contrived example because of its extreme
simplicity, but it serves to demonstrate the very basics of setting up
and using JMS.</p>
<p>We will have a single JMS Queue called <code>OrderQueue</code>, and we will have a
single <code>MessageProducer</code> sending an order message to the queue and a
single <code>MessageConsumer</code> consuming the order message from the queue.</p>
<p>The queue will be a <code>durable</code> queue, i.e. it will survive a server
restart or crash. We also want to pre-deploy the queue, i.e. specify the
queue in the server configuration so it is created automatically
without us having to explicitly create it from the client.</p>
<h1 id="jndi-configuration">JNDI Configuration</h1>
<p>The JMS specification establishes the convention that <em>administered
objects</em> (i.e. JMS queue, topic and connection factory instances) are
made available via the JNDI API. Brokers are free to implement JNDI as
they see fit assuming the implementation fits the API. Apache ActiveMQ Artemis does not
have a JNDI server. Rather, it uses a client-side JNDI implementation
that relies on special properties set in the environment to construct
the appropriate JMS objects. In other words, no objects are stored in
JNDI on the Apache ActiveMQ Artemis server, instead they are simply instantiated on the
client based on the provided configuration. Let&apos;s look at the different
kinds of administered objects and how to configure them.</p>
<blockquote>
<p><strong>Note</strong></p>
<p>The following configuration properties <em>are strictly required when
Apache ActiveMQ Artemis is running in stand-alone mode</em>. When Apache ActiveMQ Artemis is integrated
to an application server (e.g. Wildfly) the application server itself
will almost certainly provide a JNDI client with its own properties.</p>
</blockquote>
<h2 id="connectionfactory-jndi">ConnectionFactory JNDI</h2>
<p>A JMS connection factory is used by the client to make connections to
the server. It knows the location of the server it is connecting to, as
well as many other configuration parameters.</p>
<p>Here&apos;s a simple example of the JNDI context environment for a client
looking up a connection factory to access an <em>embedded</em> instance of
Apache ActiveMQ Artemis:</p>
<pre><code>java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
connectionFactory.invmConnectionFactory=vm://0
</code></pre><p>In this instance we have created a connection factory that is bound to
<code>invmConnectionFactory</code>, any entry with prefix <code>connectionFactory.</code> will
create a connection factory.</p>
<p>In certain situations there could be multiple server instances running
within a particular JVM. In that situation each server would typically
have an InVM acceptor with a unique server-ID. A client using JMS and
JNDI can account for this by specifying a connction factory for each
server, like so:</p>
<pre><code>java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
connectionFactory.invmConnectionFactory0=vm://0
connectionFactory.invmConnectionFactory1=vm://1
connectionFactory.invmConnectionFactory2=vm://2
</code></pre><p>Here is a list of all the supported URL schemes:</p>
<ul>
<li><p><code>vm</code></p>
</li>
<li><p><code>tcp</code></p>
</li>
<li><p><code>udp</code></p>
</li>
<li><p><code>jgroups</code></p>
</li>
</ul>
<p>Most clients won&apos;t be connecting to an embedded broker. Clients will
most commonly connect across a network a remote broker. Here&apos;s a simple
example of a client configuring a connection factory to connect to a
remote broker running on myhost:5445:</p>
<pre><code>java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
connectionFactory.ConnectionFactory=tcp://myhost:5445
</code></pre><p>In the example above the client is using the <code>tcp</code> scheme for the
provider URL. A client may also specify multiple comma-delimited
host:port combinations in the URL (e.g.
<code>(tcp://remote-host1:5445,remote-host2:5445)</code>). Whether there is one or
many host:port combinations in the URL they are treated as the <em>initial
connector(s)</em> for the underlying connection.</p>
<p>The <code>udp</code> scheme is also supported which should use a host:port
combination that matches the <code>group-address</code> and <code>group-port</code> from the
corresponding <code>broadcast-group</code> configured on the ActiveMQ Artemis server(s).</p>
<p>Each scheme has a specific set of properties which can be set using the
traditional URL query string format (e.g.
<code>scheme://host:port?key1=value1&amp;key2=value2</code>) to customize the
underlying transport mechanism. For example, if a client wanted to
connect to a remote server using TCP and SSL it would create a connection
factory like so, <code>tcp://remote-host:5445?ssl-enabled=true</code>.</p>
<p>All the properties available for the <code>tcp</code> scheme are described in <a href="#configuring-transports.netty">the
documentation regarding the Netty
transport</a>.</p>
<p>Note if you are using the <code>tcp</code> scheme and multiple addresses then a query
can be applied to all the url&apos;s or just to an individual connector, so where
you have</p>
<ul>
<li><code>(tcp://remote-host1:5445?httpEnabled=true,remote-host2:5445?httpEnabled=true)?clientID=1234</code></li>
</ul>
<p>then the <code>httpEnabled</code> property is only set on the individual connectors where as the <code>clientId</code>
is set on the actual connection factory. Any connector specific properties set on the whole
URI will be applied to all the connectors.</p>
<p>The <code>udp</code> scheme supports 4 properties:</p>
<ul>
<li><p><code>localAddress</code> - If you are running with multiple network
interfaces on the same machine, you may want to specify that the
discovery group listens only only a specific interface. To do this
you can specify the interface address with this parameter.</p>
</li>
<li><p><code>localPort</code> - If you want to specify a local port to which the
datagram socket is bound you can specify it here. Normally you would
just use the default value of -1 which signifies that an anonymous
port should be used. This parameter is always specified in
conjunction with <code>localAddress</code>.</p>
</li>
<li><p><code>refreshTimeout</code> - This is the period the discovery group waits
after receiving the last broadcast from a particular server before
removing that servers connector pair entry from its list. You would
normally set this to a value significantly higher than the
broadcast-period on the broadcast group otherwise servers might
intermittently disappear from the list even though they are still
broadcasting due to slight differences in timing. This parameter is
optional, the default value is 10000 milliseconds (10 seconds).</p>
</li>
<li><p><code>discoveryInitialWaitTimeout</code> - If the connection factory is used
immediately after creation then it may not have had enough time to
received broadcasts from all the nodes in the cluster. On first
usage, the connection factory will make sure it waits this long
since creation before creating the first connection. The default
value for this parameter is 10000 milliseconds.</p>
</li>
</ul>
<p>Lastly, the <code>jgroups</code> scheme is supported which provides an alternative
to the <code>udp</code> scheme for server discovery. The URL pattern is either
<code>jgroups://channelName?file=jgroups-xml-conf-filename</code>
where<code>jgroups-xml-conf-filename</code> refers to an XML file on the classpath
that contains the JGroups configuration or it can be
<code>jgroups://channelName?properties=some-jgroups-properties</code>. In both instance the
<code>channelName</code> is the name given to the jgroups channel created.</p>
<p>The <code>refreshTimeout</code> and <code>discoveryInitialWaitTimeout</code> properties
are supported just like with <code>udp</code>.</p>
<p>The default type for the default connection factory is of type <code>javax.jms.ConnectionFactory</code>.
This can be changed by setting the type like so</p>
<pre><code>java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
java.naming.provider.url=tcp://localhost:5445?type=CF
</code></pre><p>In this example it is still set to the default, below shows a list of types that can be set.</p>
<h4 id="configuration-for-connection-factory-types">Configuration for Connection Factory Types</h4>
<table>
<thead>
<tr>
<th>type</th>
<th>interface</th>
</tr>
</thead>
<tbody>
<tr>
<td>CF (default)</td>
<td>javax.jms.ConnectionFactory</td>
</tr>
<tr>
<td>XA_CF</td>
<td>javax.jms.XAConnectionFactory</td>
</tr>
<tr>
<td>QUEUE_CF</td>
<td>javax.jms.QueueConnectionFactory</td>
</tr>
<tr>
<td>QUEUE_XA_CF</td>
<td>javax.jms.XAQueueConnectionFactory</td>
</tr>
<tr>
<td>TOPIC_CF</td>
<td>javax.jms.TopicConnectionFactory</td>
</tr>
<tr>
<td>TOPIC_XA_CF</td>
<td>javax.jms.XATopicConnectionFactory</td>
</tr>
</tbody>
</table>
<h3 id="destination-jndi">Destination JNDI</h3>
<p>JMS destinations are also typically looked up via JNDI. As with
connection factories, destinations can be configured using special
properties in the JNDI context environment. The property <em>name</em> should
follow the pattern: <code>queue.&lt;jndi-binding&gt;</code> or <code>topic.&lt;jndi-binding&gt;</code>.
The property <em>value</em> should be the name of the queue hosted by the
Apache ActiveMQ Artemis server. For example, if the server had a JMS queue configured
like so:</p>
<pre><code>&lt;queue name=&quot;OrderQueue&quot;/&gt;
</code></pre><p>And if the client wanted to bind this queue to &quot;queues/OrderQueue&quot; then
the JNDI properties would be configured like so:</p>
<pre><code>java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
java.naming.provider.url=tcp://myhost:5445
queue.queues/OrderQueue=OrderQueue
</code></pre><p>It is also possible to look-up JMS destinations which haven&apos;t been
configured explicitly in the JNDI context environment. This is possible
using <code>dynamicQueues/</code> or <code>dynamicTopics/</code> in the look-up string. For
example, if the client wanted to look-up the aforementioned &quot;OrderQueue&quot;
it could do so simply by using the string &quot;dynamicQueues/OrderQueue&quot;.
Note, the text that follows <code>dynamicQueues/</code> or <code>dynamicTopics/</code> must
correspond <em>exactly</em> to the name of the destination on the server.</p>
<h3 id="the-code">The code</h3>
<p>Here&apos;s the code for the example:</p>
<p>First we&apos;ll create a JNDI initial context from which to lookup our JMS
objects. If the above properties are set in <code>jndi.properties</code> and it is
on the classpath then any new, empty <code>InitialContext</code> will be
initialized using those properties:</p>
<pre><code class="lang-java">InitialContext ic = <span class="hljs-keyword">new</span> InitialContext();
<span class="hljs-comment">//Now we&apos;ll look up the connection factory from which we can create</span>
<span class="hljs-comment">//connections to myhost:5445:</span>
ConnectionFactory cf = (ConnectionFactory)ic.lookup(<span class="hljs-string">&quot;ConnectionFactory&quot;</span>);
<span class="hljs-comment">//And look up the Queue:</span>
Queue orderQueue = (Queue)ic.lookup(<span class="hljs-string">&quot;queues/OrderQueue&quot;</span>);
<span class="hljs-comment">//Next we create a JMS connection using the connection factory:</span>
Connection connection = cf.createConnection();
<span class="hljs-comment">//And we create a non transacted JMS Session, with AUTO\_ACKNOWLEDGE</span>
<span class="hljs-comment">//acknowledge mode:</span>
Session session = connection.createSession(<span class="hljs-keyword">false</span>, Session.AUTO_ACKNOWLEDGE);
<span class="hljs-comment">//We create a MessageProducer that will send orders to the queue:</span>
MessageProducer producer = session.createProducer(orderQueue);
<span class="hljs-comment">//And we create a MessageConsumer which will consume orders from the</span>
<span class="hljs-comment">//queue:</span>
MessageConsumer consumer = session.createConsumer(orderQueue);
<span class="hljs-comment">//We make sure we start the connection, or delivery won&apos;t occur on it:</span>
connection.start();
<span class="hljs-comment">//We create a simple TextMessage and send it:</span>
TextMessage message = session.createTextMessage(<span class="hljs-string">&quot;This is an order&quot;</span>);
producer.send(message);
<span class="hljs-comment">//And we consume the message:</span>
TextMessage receivedMessage = (TextMessage)consumer.receive();
System.out.println(<span class="hljs-string">&quot;Got order: &quot;</span> + receivedMessage.getText());
</code></pre>
<p>It is as simple as that. For a wide range of working JMS examples please
see the examples directory in the distribution.</p>
<blockquote>
<p><strong>Warning</strong></p>
<p>Please note that JMS connections, sessions, producers and consumers
are <em>designed to be re-used</em>.</p>
<p>It is an anti-pattern to create new connections, sessions, producers
and consumers for each message you produce or consume. If you do this,
your application will perform very poorly. This is discussed further
in the section on performance tuning <a href="perf-tuning.html">Performance Tuning</a>.</p>
</blockquote>
<h3 id="directly-instantiating-jms-resources-without-using-jndi">Directly instantiating JMS Resources without using JNDI</h3>
<p>Although it is a very common JMS usage pattern to lookup JMS
<em>Administered Objects</em> (that&apos;s JMS Queue, Topic and ConnectionFactory
instances) from JNDI, in some cases you just think &quot;Why do I need JNDI?
Why can&apos;t I just instantiate these objects directly?&quot;</p>
<p>With Apache ActiveMQ Artemis you can do exactly that. Apache ActiveMQ Artemis supports the direct
instantiation of JMS Queue, Topic and ConnectionFactory instances, so
you don&apos;t have to use JNDI at all.</p>
<blockquote>
<p>For a full working example of direct instantiation please look at the
&quot;Instantiate JMS Objects Directly&quot; example under the JMS section of the
examples. See the <a href="examples.html">Examples</a> section for more info.</p>
</blockquote>
<p>Here&apos;s our simple example, rewritten to not use JNDI at all:</p>
<p>We create the JMS ConnectionFactory object via the ActiveMQJMSClient
Utility class, note we need to provide connection parameters and specify
which transport we are using, for more information on connectors please
see <a href="configuring-transports.html">Configuring the Transport</a>.</p>
<pre><code class="lang-java">TransportConfiguration transportConfiguration = <span class="hljs-keyword">new</span> TransportConfiguration(NettyConnectorFactory.class.getName());
ConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF,transportConfiguration);
<span class="hljs-comment">//We also create the JMS Queue object via the ActiveMQJMSClient Utility</span>
<span class="hljs-comment">//class:</span>
Queue orderQueue = ActiveMQJMSClient.createQueue(<span class="hljs-string">&quot;OrderQueue&quot;</span>);
<span class="hljs-comment">//Next we create a JMS connection using the connection factory:</span>
Connection connection = cf.createConnection();
<span class="hljs-comment">//And we create a non transacted JMS Session, with AUTO\_ACKNOWLEDGE</span>
<span class="hljs-comment">//acknowledge mode:</span>
Session session = connection.createSession(<span class="hljs-keyword">false</span>, Session.AUTO_ACKNOWLEDGE);
<span class="hljs-comment">//We create a MessageProducer that will send orders to the queue:</span>
MessageProducer producer = session.createProducer(orderQueue);
<span class="hljs-comment">//And we create a MessageConsumer which will consume orders from the</span>
<span class="hljs-comment">//queue:</span>
MessageConsumer consumer = session.createConsumer(orderQueue);
<span class="hljs-comment">//We make sure we start the connection, or delivery won&apos;t occur on it:</span>
connection.start();
<span class="hljs-comment">//We create a simple TextMessage and send it:</span>
TextMessage message = session.createTextMessage(<span class="hljs-string">&quot;This is an order&quot;</span>);
producer.send(message);
<span class="hljs-comment">//And we consume the message:</span>
TextMessage receivedMessage = (TextMessage)consumer.receive();
System.out.println(<span class="hljs-string">&quot;Got order: &quot;</span> + receivedMessage.getText());
</code></pre>
<h3 id="setting-the-client-id">Setting The Client ID</h3>
<p>This represents the client id for a JMS client and is needed for
creating durable subscriptions. It is possible to configure this on the
connection factory and can be set via the <code>clientId</code> element. Any
connection created by this connection factory will have this set as its
client id.</p>
<h3 id="setting-the-batch-size-for-dups-ok">Setting The Batch Size for DUPS_OK</h3>
<p>When the JMS acknowledge mode is set to <code>DUPS_OK</code> it is possible to
configure the consumer so that it sends acknowledgements in batches
rather that one at a time, saving valuable bandwidth. This can be
configured via the connection factory via the <code>dupsOkBatchSize</code>
element and is set in bytes. The default is 1024 * 1024 bytes = 1 MiB.</p>
<h3 id="setting-the-transaction-batch-size">Setting The Transaction Batch Size</h3>
<p>When receiving messages in a transaction it is possible to configure the
consumer to send acknowledgements in batches rather than individually
saving valuable bandwidth. This can be configured on the connection
factory via the <code>transactionBatchSize</code> element and is set in bytes.
The default is 1024 * 1024.</p>
</section>
</div>
</div>
</div>
<a href="./using-server.html" class="navigation navigation-prev " aria-label="Previous page: Using the Server"><i class="fa fa-angle-left"></i></a>
<a href="./using-core.html" class="navigation navigation-next " aria-label="Next page: Using Core"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="gitbook/app.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
gitbook.start(config);
});
</script>
</body>
</html>