aliyun - 消息队列

控制台

https://ons.console.aliyun.com

帮助文档

https://help.aliyun.com/product/29530.html

springboot示例

maven

<dependency>
    <groupId>com.aliyun.openservicesgroupId>
   <artifactId>ons-clientartifactId>
   <version>1.7.5.Finalversion>
dependency>

application.properties

ons.accessKeyId = ****************
ons.accessKeySecret = ****************
ons.host = http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet
ons.test.producerId = PID_TEST
ons.test.consumerId = CID_TEST
ond.test.topic = TOPIC_TEST

java config

@Configuration
public class OnsConfig {
    @Value("${ons.accessKeyId}")
    private String accessKeyId;
    @Value("${ons.accessKeySecret}")
    private String accessKeySecret;
    @Value("${ons.host}")
    private String onsHost;
    /**
     * 生产者
     */
    @Bean(initMethod = "start",destroyMethod = "shutdown")
    Producer testProducer(@Value("${ons.test.producerId}") String producerId){
        ProducerBean producerBean = new ProducerBean();
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.MQType, MQType.METAQ.name());
        properties.put(PropertyKeyConst.ProducerId, producerId);
        properties.put(PropertyKeyConst.AccessKey,accessKeyId);
        properties.put(PropertyKeyConst.SecretKey, accessKeySecret);
        properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "3000");
        properties.put(PropertyKeyConst.ONSAddr,onsHost);
        producerBean.setProperties(properties);
        return producerBean;
    }


    @Bean
    MessageListener testMessageListener(){
        return new TestMessageListener();   
    }

    /**
     * 消息者
     */
    @Bean(initMethod = "start",destroyMethod = "shutdown")
    Consumer testConsumer(
        @Value("${ons.test.consumerId}") String consumerId,
        @Value("${ons.test.topic}") String topic,
        @Qualifier("testMessageListener") MessageListener messageListener
        ) {
        ConsumerBean consumerBean = new ConsumerBean();
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.MQType, MQType.METAQ.name());
        properties.put(PropertyKeyConst.ConsumerId, consumerId);
        properties.put(PropertyKeyConst.AccessKey,accessKeyId);
        properties.put(PropertyKeyConst.SecretKey, accessKeySecret);
        properties.put(PropertyKeyConst.ONSAddr,onsHost);
        properties.put(PropertyKeyConst.MaxReconsumeTimes,"3");
        consumerBean.setProperties(properties);

        Map subscriptionTable = new HashMap<>(16);
        Subscription subscription = new Subscription();
        subscription.setTopic(topic);
        subscription.setExpression("TagA");
       subscriptionTable.put(subscription,messageListener);
        consumerBean.setSubscriptionTable(subscriptionTable);
        return consumerBean;
    }

produce message

@Component
public class OnsSupportComponentImpl implements OnsSupportComponent {

    @Autowired
    @Qualifier("testProducer")
    private Producer producer;

    @Value("${ons.test.topic}")
    private String topic;

    @Override
    public void sendAsyncMessageForTest(String body) {
        Message message = new Message(topic, "TagA", RandomUtils.nextInt() + "", body.getBytes());
        producer.sendAsync(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
            }

            @Override
            public void onException(OnExceptionContext context) {
            }
        });
    }
}

cosumer message

public class TestMessageListener implements MessageListener{

    @Override
    public Action consume(Message message, ConsumeContext context) {
        try{
            String body = new String(message.getBody());
            System.out.println(body);
            //消费逻辑代码
        }catch (Exception ex){
            return Action.ReconsumeLater;
        }
        return Action.CommitMessage;
    }
}

你可能感兴趣的:(java,aliyun,middle)