您可以查看JMS SDK的獲取方式、使用限制和示例代碼。
準備工作
在本地目錄中創建.aliyun-mns.properties文件,并填寫服務地址、AccessKeyId和AccessKeySecret。
說明
Linux系統本地目錄為/home/YOURNAME/,Windows系統本地目錄為C:\Users\YOURNAME。
mns.accountendpoint=http://<yourAccountId>.mns.cn-hangzhou.aliyuncs.com
mns.accesskeyid=<yourAccessKeyId>
mns.accesskeysecret=<yourAccessKeySecret>
AccessKeyId、AccessKeySecret
訪問阿里云API的密鑰對。
如果使用阿里云賬號訪問,請登錄AccessKey管理頁面創建和查看。
如果使用RAM用戶訪問,請登錄阿里云訪問控制控制臺查看。
Endpoint
獲取方式
消息服務MNS JMS庫的獲取方式如下:
在Maven項目的pom.xml文件中添加依賴
<dependency> <groupId>com.aliyun.mns</groupId> <artifactId>java-messaging-lib</artifactId> <version>0.2.0</version> </dependency>
使用限制
只支持創建隊列和收發消息操作。
只支持AUTO_ACKNOWLEDGE和MANUAL_ACKNOWLEDGE兩種ACK模式:
AUTO_ACKNOWLEDGE:
MessageListener執行完成且無異常拋出時會自動ACK。
MessageListener拋出異常就不會ACK,在VisibilityTimeout后消息會重投。
MANUAL_ACKNOWLEDGE:您需要在代碼中調用
acknowledge()
對消息進行ACK。
示例代碼
創建隊列
String accessKeyId = ServiceSettings.getMNSAccessKeyId(); String accessKeySecret = ServiceSettings.getMNSAccessKeySecret(); String endpoint = ServiceSettings.getMNSAccountEndpoint(); 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);
發送普通消息
String accessKeyId = ServiceSettings.getMNSAccessKeyId(); String accessKeySecret = ServiceSettings.getMNSAccessKeySecret(); String endpoint = ServiceSettings.getMNSAccountEndpoint(); 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(); }
發送延遲消息
String accessKeyId = ServiceSettings.getMNSAccessKeyId(); String accessKeySecret = ServiceSettings.getMNSAccessKeySecret(); String endpoint = ServiceSettings.getMNSAccountEndpoint(); 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); //設置DelaySeconds。 MNSMessageHelper.setDelaySeconds(textMessage, 10); producer.send(textMessage); System.out.println(textMessage.getJMSMessageID()); } catch (JMSException e) { e.printStackTrace(); }
AUTO_ACKNOWLEDGE模式消費
String accessKeyId = ServiceSettings.getMNSAccessKeyId(); String accessKeySecret = ServiceSettings.getMNSAccessKeySecret(); String endpoint = ServiceSettings.getMNSAccountEndpoint(); 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(); }
MANUAL_ACKNOWLEDGE模式消費消息
String accessKeyId = ServiceSettings.getMNSAccessKeyId(); String accessKeySecret = ServiceSettings.getMNSAccessKeySecret(); String endpoint = ServiceSettings.getMNSAccountEndpoint(); 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()); } //消費成功后需手動ACK。 message.acknowledge(); } catch (JMSException e) { e.printStackTrace(); } } }; consumer.setMessageListener(listener); connection.start(); } catch (Exception e) { e.printStackTrace(); }
文檔內容是否對您有幫助?