This article describes the obtaining methods, limits, and sample code of SDK for Java Message Service (JMS).

Obtaining methods

You can use one of the following methods to obtain MNS SDK for JMS:

  • Add the following dependency to the pom.xml file of the Maven project:
    <dependency>
        <groupId>com.aliyun.mns</groupId>
        <artifactId>java-messaging-lib</artifactId>
        <version>0.2.0</version>
    </dependency>
  • Download the JAR file of the dependency

Limits

  • SDK for JMS can be used only to create queues, send messages, and receive messages.
  • Only the AUTO_ACKNOWLEDGE and MANUAL_ACKNOWLEDGE ACK modes are supported.
    • AUTO_ACKNOWLEDGE:
      • If MessageListener is executed and does not throw exceptions, messages are automatically acknowledged.
      • If MessageListener throws an exception, messages are not acknowledged. After the VisibilityTimeout expires, messages are re-sent.
    • MANUAL_ACKNOWLEDGE: You must call the acknowledge() method to acknowledge messages.

Sample code

  • Create a queue
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    String endpoint = "<yourEndpoint>";
    String queueName = "<yourQueueName>";
    
    MNSConnectionFactory factory = MNSConnectionFactory.builder()
        .withAccessKeyId(accessKeyId)
        .withAccessKeySecret(accessKeySecret)
        .withEndpoint(endpoint)
        .build();
    
    MNSQueueConnection connection = factory.createQueueConnection();
    
    MNSClientWrapper mnsClientWrapper = connection.getMNSClientWrapper();
    mnsClientWrapper.createQueue(queueName);
  • Send normal messages
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    String endpoint = "<yourEndpoint>";
    String queueName = "<yourQueueName>";
    
    MNSConnectionFactory factory = MNSConnectionFactory.builder()
        .withAccessKeyId(accessKeyId)
        .withAccessKeySecret(accessKeySecret)
        .withEndpoint(endpoint)
        .build();
    
    try {
        MNSQueueConnection connection = factory.createQueueConnection();
        QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    
        Queue queue = session.createQueue(queueName);
        MessageProducer producer = session.createProducer(queue);
    
        TextMessage textMessage = session.createTextMessage("Hello JMS! ");
        textMessage.setDoubleProperty("TestFloat", 0.127);
        producer.send(textMessage);
    
        System.out.println(textMessage.getJMSMessageID());
    } catch (JMSException e) {
        e.printStackTrace();
    }
  • Send delayed messages
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    String endpoint = "<yourEndpoint>";
    String queueName = "<yourQueueName>";
    
    MNSConnectionFactory factory = MNSConnectionFactory.builder()
        .withAccessKeyId(accessKeyId)
        .withAccessKeySecret(accessKeySecret)
        .withEndpoint(endpoint)
        .build();
    
    try {
        MNSQueueConnection connection = factory.createQueueConnection();
        QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    
        Queue queue = session.createQueue(queueName);
        MessageProducer producer = session.createProducer(queue);
    
        TextMessage textMessage = session.createTextMessage("Hello JMS! ");
        textMessage.setDoubleProperty("TestFloat", 0.127);
        // Set the DelaySeconds parameter.
        MNSMessageHelper.setDelaySeconds(textMessage, 10);
        producer.send(textMessage);
    
        System.out.println(textMessage.getJMSMessageID());
    } catch (JMSException e) {
        e.printStackTrace();
    }
  • Consume messages in AUTO_ACKNOWLEDGE mode
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    String endpoint = "<yourEndpoint>";
    String queueName = "<yourQueueName>";
    
    MNSConnectionFactory factory = MNSConnectionFactory.builder()
        .withAccessKeyId(accessKeyId)
        .withAccessKeySecret(accessKeySecret)
        .withEndpoint(endpoint)
        .build();
    
    try {
        MNSQueueConnection connection = factory.createQueueConnection();
        QueueSession session = connection.createQueueSession(false, MNSQueueSession.AUTO_ACKNOWLEDGE);
    
        Queue queue = session.createQueue(queueName);
        MessageConsumer consumer = session.createConsumer(queue);
        MessageListener listener = new MessageListener() {
    
            @Override
            public void onMessage(Message message) {
                try {
                    if (message instanceof TextMessage) {
                        MNSTextMessage textMessage = (MNSTextMessage) message;
    
                        System.out.println(new Date() + " Receive in listener: " + textMessage.getText());
                    } else if (message instanceof BytesMessage) {
                        MNSBytesMessage bytesMessage = (MNSBytesMessage) message;
                        char readChar = bytesMessage.readChar();
                        System.out.println("Read Char: " + readChar);
                        int readInt = bytesMessage.readInt();
                        System.out.println("Read Int: " + readInt);
                    } else if (message instanceof ObjectMessage) {
                        MNSObjectMessage objectMessage = (MNSObjectMessage) message;
                        TestSerializable object = (TestSerializable) objectMessage.getObject();
                        System.out.println(object.getName());
                    }
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        };
    
        consumer.setMessageListener(listener);
        connection.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
  • Consume messages in MANUAL_ACKNOWLEDGE mode
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    String endpoint = "<yourEndpoint>";
    String queueName = "<yourQueueName>";
    
    MNSConnectionFactory factory = MNSConnectionFactory.builder()
        .withAccessKeyId(accessKeyId)
        .withAccessKeySecret(accessKeySecret)
        .withEndpoint(endpoint)
        .build();
    
    try {
        MNSQueueConnection connection = factory.createQueueConnection();
        QueueSession session = connection.createQueueSession(false, MNSQueueSession.MANUAL_ACKNOWLEDGE);
    
        Queue queue = session.createQueue(queueName);
        MessageConsumer consumer = session.createConsumer(queue);
        MessageListener listener = new MessageListener() {
    
            @Override
            public void onMessage(Message message) {
                try {
                    if (message instanceof TextMessage) {
                        MNSTextMessage textMessage = (MNSTextMessage) message;
    
                        System.out.println(new Date() + " Receive in listener: " + textMessage.getText());
                    } else if (message instanceof BytesMessage) {
                        MNSBytesMessage bytesMessage = (MNSBytesMessage) message;
                        char readChar = bytesMessage.readChar();
                        System.out.println("Read Char: " + readChar);
                        int readInt = bytesMessage.readInt();
                        System.out.println("Read Int: " + readInt);
                    } else if (message instanceof ObjectMessage) {
                        MNSObjectMessage objectMessage = (MNSObjectMessage) message;
                        TestSerializable object = (TestSerializable) objectMessage.getObject();
                        System.out.println(object.getName());
                    }
    
                    // After the consumption succeeds, manually acknowledge messages.
                    message.acknowledge();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        };
    
        consumer.setMessageListener(listener);
        connection.start();
    } catch (Exception e) {
        e.printStackTrace();
    }