日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

JMS SDK

您可以查看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

  • Endpoint

    • 訪問消息服務MNS的接入地址,登錄MNS控制臺查看。具體操作,請參見獲取接入點

    • 不同地域的接入地址不同,分為公網和私網域名。

獲取方式

消息服務MNS JMS庫的獲取方式如下:

  • 在Maven項目的pom.xml文件中添加依賴

    <dependency>
        <groupId>com.aliyun.mns</groupId>
        <artifactId>java-messaging-lib</artifactId>
        <version>0.2.0</version>
    </dependency>
  • 下載依賴的JAR文件

使用限制

  • 只支持創建隊列和收發消息操作。

  • 只支持AUTO_ACKNOWLEDGEMANUAL_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();
    }