智慧停車(七) 使用RocketMQ實現定時消息

既然決定了用RocketMQ實現定時消息,就立馬準備環境開始測試。爲了省事直接在阿里雲官網上買個RocketMQ簡簡單單,下面將整個過程分享下。

分享個2000元代金卷大禮包: https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=5gg22qjx

一 購買RocketMQ

在阿里雲官網找到消息隊列RocketMQ,進入購買頁面,我直接買的是包月的,先買1個月試試,這裏要注意的是RocketMQ資源包分Topic資源和API請求資源兩塊都是需要單獨付費的。

 

二 配置RocketMQ

注意:以下3個配置必須選擇在統一區域下,測試階段建議選擇公網。

1.創建實例

2.創建Topic


3.創建GroupID


三 測試RocketMQ

1.發送定時消息

@RequestMapping(value="/producerDelay")
	public void producerDelay() {
		
	Properties properties = new Properties();
        // 阿里雲身份驗證,在阿里雲服務器管理控制檯創建
        properties.put(PropertyKeyConst.AccessKey, accessKeyID);
        // 阿里雲身份驗證,在阿里雲服務器管理控制檯創建
        properties.put(PropertyKeyConst.SecretKey, accessKeySecret);
        // 設置 TCP 接入域名,進入控制檯的實例管理頁面的“獲取接入點信息”區域查看
        properties.put(PropertyKeyConst.NAMESRV_ADDR,"http://test.mq-internet-access.mq-internet.aliyuncs.com:80");
        Producer producer = ONSFactory.createProducer(properties);
        // 在發送消息前,必須調用 start 方法來啓動 Producer,只需調用一次即可。
        producer.start();
        
        Message msg = new Message( 
                // Message 所屬的 Topic
                "topic-delay",
                // Message Tag 可理解爲 Gmail 中的標籤,對消息進行再歸類,方便 Consumer 指定過濾條件在消息隊列 RocketMQ 的服務器過濾
                "tag-delay",
                // Message Body 可以是任何二進制形式的數據, 消息隊列 RocketMQ 不做任何干預,需要 Producer 與 Consumer 協商好一致的序列化和反序列化方式
                "Hello MQ".getBytes());
        
        // 設置代表消息的業務關鍵屬性,請儘可能全局唯一
        // 以方便您在無法正常收到消息情況下,可通過控制檯查詢消息並補發。
        // 注意:不設置也不會影響消息正常收發
        msg.setKey("ORDERID_100");
        
        try {
            // 定時消息,單位毫秒(ms),在指定時間戳(當前時間之後)進行投遞,例如 2016-03-07 16:21:00 投遞。如果被設置成當前時間戳之前的某個時刻,消息將立刻投遞給消費者。
            long timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-03-07 16:21:00").getTime();
            msg.setStartDeliverTime(timeStamp);
            // 發送消息,只要不拋異常就是成功
            SendResult sendResult = producer.send(msg);
            System.out.println("Message Id:" + sendResult.getMessageId());
        }
        catch (Exception e) {
            // 消息發送失敗,需要進行重試處理,可重新發送這條消息或持久化這條數據進行補償處理
            System.out.println(new Date() + " Send mq message failed. Topic is:" + msg.getTopic());
            e.printStackTrace();
        }
        // 在應用退出前,銷燬 Producer 對象
        // 注意:如果不銷燬也沒有問題
        producer.shutdown();
		
	}

 

 2.訂閱消息

@PostConstruct  
    public void subscribe(){
		try{	
			
		Properties properties = new Properties();
	        // 您在控制檯創建的 Group ID
	        properties.put(PropertyKeyConst.GROUP_ID, "GID_delay");
	        // AccessKey 阿里雲身份驗證,在阿里雲服務器管理控制檯創建
	        properties.put(PropertyKeyConst.AccessKey, accessKeyID);
	        // SecretKey 阿里雲身份驗證,在阿里雲服務器管理控制檯創建
	        properties.put(PropertyKeyConst.SecretKey, accessKeySecret);
	        // 設置 TCP 接入域名,進入控制檯的實例管理頁面的“獲取接入點信息”區域查看
	        properties.put(PropertyKeyConst.NAMESRV_ADDR, "http://test.mq-internet-access.mq-internet.aliyuncs.com:80");
	        // 集羣訂閱方式 (默認)
	        //properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.CLUSTERING);
	        Consumer consumer = ONSFactory.createConsumer(properties);
	        
	       //訂閱全部 Tag方式
	       consumer.subscribe("topic-delayMessage", "*", new MessageListener() {
	            public Action consume(Message message, ConsumeContext context) {
	                System.out.println("Receive: " + message);
	                System.out.println("topic: " + message.getTopic());
	                System.out.println("key: " + message.getKey());
	                System.out.println("tag: " + message.getTag());
	                System.out.println("body: " + new String(message.getBody()));
	                return Action.CommitMessage;
	            }
	        });
	        
	       consumer.start();
	       System.out.println("Consumer Started");
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章