このコンテンツは選択した言語では利用できません。
Chapter 5. Examples
This chapter demonstrates the use of AMQ JMS through example programs. To run them, make sure you have completed the steps in the Chapter 2, Installation chapter for your environment and you have a running and configured broker.
See the Qpid JMS examples for more sample programs.
5.1. Configuring the JNDI Context リンクのコピーリンクがクリップボードにコピーされました!
Applications using JMS typically use JNDI to obtain the ConnectionFactory and Destination objects used by the application. This keeps the configuration separate from the program and insulates it from the particular client implementation.
For the purpose of using these examples, a file named jndi.properties should be placed on the classpath to configure the JNDI Context, as detailed previously.
The contents of the jndi.properties file should match what is shown below, which as per the format described previously establishes that the client’s InititalContextFactory implementation should be used, configures a ConnectionFactory to connect to a local server, and defines a destination queue named queue.
5.2. Sending Messages リンクのコピーリンクがクリップボードにコピーされました!
This example first creates a JNDI Context, uses it to look up a ConnectionFactory and Destination, creates and starts a Connection using the factory, and then creates a Session. Then a MessageProducer is created to the Destination, and a message is sent using it. The Connection is then closed, and the program exits.
A runnable variant of this Sender example is in the INSTALL_DIR/examples directory, along with the Hello World example covered previously in Chapter 3, Getting Started.
Example: Sending Messages
- 1
- Creates the JNDI
Contextto look upConnectionFactoryandDestinationobjects. The configuration is picked up from thejndi.propertiesfile as detailed earlier. - 2
- The ConnectionFactory and Destination objects are retrieved from the JNDI Context using their lookup names.
- 3
- The factory is used to create the
Connection, which then has anExceptionListenerregistered and is then started. The credentials given when creating the connection will typically be taken from an appropriate external configuration source, ensuring they remain separate from the application itself and can be updated independently. - 4
- A non-transacted, auto-acknowledge
Sessionis created on theConnection. - 5
- The
MessageProduceris created to send messages to theDestination. - 6
- A
TextMessageis created with the given content. - 7
- The
TextMessageis sent. It is sent non-persistent, with default priority and no expiration. - 8
- The
Connectionis closed. TheSessionandMessageProducerare closed implicitly.
Note that this is only an example. A real-world application would typically use a long-lived MessageProducer and send many messages using it over time. Opening and then closing a Connection, Session, and MessageProducer per message is generally not efficient.
5.3. Receiving Messages リンクのコピーリンクがクリップボードにコピーされました!
This example starts by creating a JNDI Context, using it to look up a ConnectionFactory and Destination, creating and starting a Connection using the factory, and then creates a Session. Then a MessageConsumer is created for the Destination, a message is received using it, and its contents are printed to the console. The Connection is then closed and the program exits. The same JNDI configuration is used as in the sending example.
An executable variant of this Receiver example is contained within the examples directory of the client distribution, along with the Hello World example covered previously in Chapter 3, Getting Started.
Example: Receiving Messages
- 1
- Creates the JNDI
Contextto look upConnectionFactoryandDestinationobjects. The configuration is picked up from thejndi.propertiesfile as detailed earlier. - 2
- The
ConnectionFactoryandDestinationobjects are retrieved from the JNDIContextusing their lookup names. - 3
- The factory is used to create the
Connection, which then has anExceptionListenerregistered and is then started. The credentials given when creating the connection will typically be taken from an appropriate external configuration source, ensuring they remain separate from the application itself and can be updated independently. - 4
- A non-transacted, auto-acknowledge
Sessionis created on theConnection. - 5
- The
MessageConsumeris created to receive messages from theDestination. - 6
- A call to receive a message is made with a five second timeout.
- 7
- The result is checked, and if a message was received, its contents are printed, or notice that no message was received. The result is cast explicitly to
TextMessageas this is what we know theSendersent. - 8
- The
Connectionis closed. TheSessionandMessageConsumerare closed implicitly.
Note that this is only an example. A real-world application would typically use a long-lived MessageConsumer and receive many messages using it over time. Opening and then closing a Connection, Session, and MessageConsumer for each message is generally not efficient.