RabbitMQ的簡單用法

這裏主要是講解的將一條消息放入mq中,然後在mq中進行處理,
生產者:
第一步:創建Queue名:

public class ConstantMqName {
    //查詢記錄隊列
    public static final String MQ_RECORD_SEARCH_CONTENT= "mq-record-search-content";
}
import com.hisi.ie.common.constant.ConstantMqName;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    @Bean
    public Queue searchContentQueue() {
        return new Queue(ConstantMqName.MQ_RECORD_SEARCH_CONTENT, true);
    }
}

第二步:將要發送的消息封裝成對象放進隊列裏面:
(這裏的消息的封裝根據需求來的)

import org.springframework.amqp.core.AmqpTemplate;
@Service
public class HisiSearchServiceImpl implements HisiSearchService {
   @Autowired
   private AmqpTemplate rabbitTemplate;
   
   //組裝寫入mq的數據
   HisiRecordSearchParam paramToMQ = new HisiRecordSearchParam();
   paramToMQ.setContent(content);
   paramToMQ.setSearchEngine(queryFieldList);
   paramToMQ.setSearchs(hisiIeSearchParam.getSearchs());
   paramToMQ.setFilters(filters);
   paramToMQ.setUserId(hisiIeSearchParam.getUserId());
   logger.info("paramToMQ ====>> " + JSONObject.toJSONString(paramToMQ));
   rabbitTemplate.convertAndSend(ConstantMqName.MQ_RECORD_SEARCH_CONTENT, JSONObject.toJSONString(paramToMQ));
 

消費者進行消費:
第一步:創建Queue名

import com.hisi.ie.common.constant.ConstantMqName;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    @Bean
    public Queue searchContentQueue() {
        return new Queue(ConstantMqName.MQ_RECORD_SEARCH_CONTENT, true);
    }
}

第二步:消費者對接收到的消息進行具體的處理
(這裏是將接收到的消息做了處理,將處理後的結果寫入redis緩存中)

@Component
public class RecordSearchReceiver {
    private static Logger logger = LoggerFactory.getLogger(RecordSearchReceiver.class);

    @Autowired
    private SearchRecordMapper searchRecordMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    //這裏的註解很重要,監聽具體的某一個消息隊列
    @RabbitHandler
    @RabbitListener(queues = ConstantMqName.MQ_RECORD_SEARCH_CONTENT)
    public void process(String message) {
        logger.info("接收到搜索記錄,開始處理[" + message + "]");
        try {
            SearchRecord searchVo = new SearchRecord();
            CombineSearchVo combineSearchVo = null;
            Set<String> content = new HashSet<>();
            //對接收到的消息進行處理,到此就是消費者接收到的消息,後面根據要求對消息做處理
            HisiRecordSearchParam recordSearch = JSON.parseObject(message, HisiRecordSearchParam.class);
            String searchContent = recordSearch.getContent();
            content.add(searchContent );
            contentToRedis(content);

        } catch (Exception e) {
            logger.error("搜索記錄入庫異常!", e);
        }
        logger.info("搜索記錄入庫結束!");
    }

    private void contentToRedis(Set<String> content) {
        Date d = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        long rightTime = (long) (d.getTime() + 8 * 60 * 60 * 1000);
        String dateNowStr = sd.format(rightTime);
        String key = "searchContent-" + dateNowStr;
        try {
            for (String keyParam : content) {
            	//序列化
                stringRedisTemplate.setValueSerializer(new StringRedisSerializer());
                //這裏緩存的是詞彙以及對應的詞頻
                stringRedisTemplate.opsForHash().increment(key, keyParam, 1);
            }
        } catch (Exception e) {
            logger.error("寫入redis緩存異常!", e);
        }
    }

前面講解的是rabbitmq,
現在獲取redis緩存中的數據(也可以通過quartz定時調度去獲取redis緩存中的數據)

 Date d = new Date();
 SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
 long rightTime = (long) (d.getTime() + 8 * 60 * 60 * 1000);
 String dateNowStr = sd.format(rightTime);
 String key = "searchContent-" + dateNowStr;
 //這裏存儲的結構類似(key,(key,value))格式,通過key名進行獲取對應的結果
 Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(key)         

大概的用法就是這樣的,做項目的時候設計好幾個服務,不好把代碼全部呈現上來,這裏是關鍵內容,至於具體的處理就根據業務邏輯來吧!

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