这里主要是讲解的将一条消息放入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)
大概的用法就是这样的,做项目的时候设计好几个服务,不好把代码全部呈现上来,这里是关键内容,至于具体的处理就根据业务逻辑来吧!