이 콘텐츠는 선택한 언어로 제공되지 않습니다.

2.3. JMS Development


Overview

Developing applications using the JMS APIs is a straightforward process. The APIs facilitate the creation of objects that mitigate the interface between client applications and the message broker. Once connected to a broker, clients can create, send and receive messages.

Basic application components

A JMS application typically consist of these basic interfaces and classes:
Connection factory
Connection factories are administered objects that clients use to create connections to a message broker. You can optimize some messaging characteristics by enabling, disabling, or otherwise modifying the values of certain connection factory properties.
Connection
Connections are the objects clients use to specify a transport protocol and credentials for sustained interaction with a broker.
Session
Sessions are created by a client on a connection established with a broker. They define whether its messages will be transacted and the acknowledgement mode when they are not. Clients can create multiple sessions on a single connection.
Destinations
Destinations are created by a client on a per-session basis. They are client-side representations of the queue or topic to which messages are sent. The message broker maintains its own representations of the destinations as well.
Producer
Producers are client-side objects that create and send messages to a broker. They are instances the MessageProducer interface and are created on a per-session basis.
The MessageProducer interface provides methods not only for sending messages, but also for setting various message headers, including JMSDeliveryMode which controls message persistence, JMSPriority which controls message priority, and JMSExpiration which controls a message's lifespan.
Consumer
Consumers are client-side objects that process messages retrieved from a broker. They are instances of the MessageConsumer interface and are created on a per-session basis.
The MessageConsumer interface can consume messages synchronously by using one of the MessageConsumer.receive() methods, or asynchronously by registering a MessageListener with the MessageConsumer.setMessageListener() method. With a MessageListener registered on a destination, messages arriving at the destination invoke the consumer's MessageListener.onMessage() method, freeing the consumer from having to repeatedly poll the destination for messages.
Messages
Messages are the backbone of a messaging system. They are objects that contain not only the data payload, but also the required header and optional properties needed to transfer them from one client application to another. See Section 2.2, “JMS Message Basics”.

Development steps

Figure 2.2, “Messaging sequence” shows the typical sequence of events involved in sending or receiving messages in a Red Hat JBoss A-MQ client application.

Figure 2.2. Messaging sequence

flow chart of steps in developing a messaging application
The following procedure shows how to implement the sequence of events shown in Figure 2.2, “Messaging sequence”:
  1. Get a connection factory.
    The process for getting a connection factory depends on your environment. It is typical to use JNDI to obtain the connection factory. JBoss A-MQ allows you to instantiate connection factories directly in consumer code or obtain connection factories using Spring in addition to using JNDI.
  2. Create a connection from the connection factory as shown in Example 2.1, “Getting a Connection”.

    Example 2.1. Getting a Connection

    connection=connectionFactory.createConnection();
  3. Start the connection using the connection's start() method.
  4. Create a session from the connection.

    Example 2.2. Creating a Session

    session=connection.createSession(false,
                                     Session.AUTO_ACKNOWLEDGE);
  5. Create a destination from the session.
    Example 2.3, “Creating a Queue” shows code for createing a queue called foo.

    Example 2.3. Creating a Queue

    destination=session.createQueue("FOO.QUEUE");
  6. Create objects for producing and consuming messages.
    1. Create a producer as shown in Example 2.4, “Creating a Message Producer”.

      Example 2.4. Creating a Message Producer

      producer=session.createProducer(destination);
    2. Create a consumer as shown in Example 2.5, “Creating a Consumer”.

      Example 2.5. Creating a Consumer

      consumer=session.createConsumer(destination);
  7. Create a message from the session object.
    Example 2.6, “Creating a Text Message” shows code for creating a text message.

    Example 2.6. Creating a Text Message

    message=session.createTextMessage("This is a foo test.");
  8. Send and receive messages.
    1. Send messages from the producer as shown in Example 2.7, “Sending a Message”.

      Example 2.7. Sending a Message

      producer.send(message);
    2. Receive messages from the consumer.
      Example 2.8, “Receiving a Message” shows code for synchronously receiving a text message.

      Example 2.8. Receiving a Message

      message = (TextMessage)consumer.receive();
  9. Close all JMS resources.
Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.