Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
© 2022 Yandex.Cloud LLC
Yandex Message Queue
  • Getting started
    • Quick start
    • Supported tools
    • Code samples
      • Node.js
      • PHP
      • JMS
      • Laravel
      • Terraform
  • Step-by-step instructions
    • Overview
    • Creating a new message queue
    • Sending messages
    • Receiving and deleting messages
    • Deleting a message queue
    • Monitoring processes in queues
  • Concepts
    • Overview
    • Message queues
    • Messages
    • Deduplication
    • Visibility timeout
    • Long Polling
    • Dead Letter Queue
    • Delay queues
    • Quotas and limits
  • Practical guidelines
    • Converting a video to a GIF in Python
  • Access management
  • Pricing policy
  • Yandex Message Queue API
    • Overview
    • Queue
      • CreateQueue
      • DeleteQueue
      • GetQueueAttributes
      • GetQueueUrl
      • ListQueues
      • PurgeQueue
      • SetQueueAttributes
    • Message
      • ChangeMessageVisibility
      • ChangeMessageVisibilityBatch
      • DeleteMessage
      • DeleteMessageBatch
      • ReceiveMessage
      • SendMessage
      • SendMessageBatch
    • Data types
      • BatchResultErrorEntry
      • ChangeMessageVisibilityBatchRequestEntry
      • ChangeMessageVisibilityBatchResultEntry
      • DeleteMessageBatchRequestEntry
      • DeleteMessageBatchResultEntry
      • Message
      • MessageAttributeValue
      • SendMessageBatchRequestEntry
      • SendMessageBatchResultEntry
    • Common errors
  • Questions and answers
  1. Getting started
  2. Code samples
  3. JMS

Example of using Yandex Message Queue on JMS

Written by
Yandex Cloud
  • Installation
  • Before you start
  • Example

JMS is an API for sending messages between application components. With the AWS SQS Java Messaging Library, you can use Message Queue to send and receive messages via JMS.

Installation

Install the SDK for Java by following the instructions on the official website.

Before you start

  1. Create a service account.
  2. Assign a role to a editor service account.
  3. Create a static access key.

Set the environment variables:

$ export AWS_ACCESS_KEY_ID="<access key ID>"
$ export AWS_SECRET_ACCESS_KEY="<secret key>"

Create a queue in Message Queue and prepare its URL.

Example

In this example:

  1. A connection with Message Queue is established.
  2. A message queue is created.
  3. A message with the text test message is sent to the queue.
  4. The message is read from the queue and displayed in the terminal.

To read about the features not covered by the example, see the documentation on the AWS SQS Java Messaging Library website.

package ru.yandex.cloud.message_queue;

import com.amazon.sqs.javamessaging.AmazonSQSMessagingClientWrapper;
import com.amazon.sqs.javamessaging.SQSConnection;
import com.amazon.sqs.javamessaging.SQSConnectionFactory;
import com.amazon.sqs.javamessaging.ProviderConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;

import javax.jms.*;

public class App
{
    private static String queueName = "ymq_jms_example";

    public static void main( String[] args ) throws JMSException
    {
        SQSConnectionFactory connectionFactory = new SQSConnectionFactory(
                new ProviderConfiguration(),
                AmazonSQSClientBuilder.standard()
                        .withRegion("ru-central1")
                        .withEndpointConfiguration(new EndpointConfiguration(
                            "https://message-queue.api.cloud.yandex.net",
                            "ru-central1"
                        ))
        );

        SQSConnection connection = connectionFactory.createConnection();

        AmazonSQSMessagingClientWrapper client = connection.getWrappedAmazonSQSClient();

        if( !client.queueExists(queueName) ) {
            client.createQueue( queueName );
        }

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Queue queue = session.createQueue(queueName);

        MessageProducer producer = session.createProducer(queue);

        Message message = session.createTextMessage("test message");
        producer.send(message);

        MessageConsumer consumer = session.createConsumer(queue);
        connection.start();
        message = consumer.receive(1000);
        System.out.println(((TextMessage) message).getText());
    }
}

Was the article helpful?

Language / Region
© 2022 Yandex.Cloud LLC
In this article:
  • Installation
  • Before you start
  • Example