com.redismq.server.controller.QueueController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redismq-server Show documentation
Show all versions of redismq-server Show documentation
redismq server and console
The newest version!
package com.redismq.server.controller;
import com.alibaba.fastjson.JSONValidator;
import com.redismq.server.pojo.MQMessageDTO;
import com.redismq.server.pojo.PageResult;
import com.redismq.server.pojo.QueuePageSelect;
import com.redismq.server.pojo.VQueue;
import com.redismq.common.connection.RedisMQClientUtil;
import com.redismq.common.constant.RedisMQConstant;
import com.redismq.common.pojo.Message;
import com.redismq.common.pojo.PushMessage;
import com.redismq.common.pojo.Queue;
import com.redismq.common.serializer.JsonSerializerUtil;
import com.redismq.utils.RedisMQTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static com.redismq.common.constant.GlobalConstant.V_QUEUE_SPLITE;
@RestController
@RequestMapping("/queue")
@Slf4j
public class QueueController {
@Autowired
private RedisMQTemplate redisMQTemplate;
@Autowired
private RedisMQClientUtil redisMQClientUtil;
@GetMapping("page")
public ResponseEntity> page(QueuePageSelect queuePageSelect) {
//队列名就是topic名
Set allQueue = redisMQClientUtil.getQueueList();
allQueue=allQueue.stream().filter(a->a.isDelayState()==queuePageSelect.isDelayState()).collect(Collectors.toSet());
int start = (queuePageSelect.getPage() - 1) * queuePageSelect.getSize();
int end = queuePageSelect.getPage() * queuePageSelect.getSize();
List page = limitPage(new ArrayList<>(allQueue), start, end);
return ResponseEntity.ok(PageResult.success(allQueue.size(),page));
}
//根据队列名称查询虚拟队列
@GetMapping("vQueueList")
public ResponseEntity> vQueueList(String queueName, Integer virtual) {
List virtualQueues = new ArrayList<>();
for (int i = 0; i < virtual; i++) {
VQueue vQueue = new VQueue();
String virtualQueue = queueName + V_QUEUE_SPLITE + i;
Long size = redisMQClientUtil.queueSize(virtualQueue);
vQueue.setQueueName(virtualQueue);
vQueue.setSize(size == null ? 0 : size);
virtualQueues.add(vQueue);
}
return ResponseEntity.ok(virtualQueues);
}
/**
* 指定队列重新拉取消息 TODO前端未接入
*
* @return {@link ResponseEntity}<{@link String}>
*/
@PostMapping("publishPullMessage")
public ResponseEntity publishPullMessage(String vQueue) {
if (vQueue == null) {
throw new RuntimeException();
}
String vQueueNameByVQueue = RedisMQConstant.getVQueueNameByVQueue(vQueue);
Boolean lock = redisMQClientUtil.isLock(RedisMQConstant.getVirtualQueueLock(vQueue));
if (lock){
throw new RuntimeException("队列正在被锁定");
}
PushMessage pushMessage = new PushMessage();
pushMessage.setTimestamp(System.currentTimeMillis());
pushMessage.setQueue(vQueueNameByVQueue);
redisMQClientUtil.publishPullMessage(pushMessage);
log.info("publishPullMessage :{}",vQueue);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@PostMapping("sendMessage")
public ResponseEntity sendMessage(@RequestBody MQMessageDTO message) {
Object body = message.getBody();
if (body instanceof String){
String str = (String) body;
if (JsonSerializerUtil.isJson(str)){
boolean validate = JSONValidator.from(str).validate();
if (!validate) {
return ResponseEntity.ok(false);
}
}
}
Message build = Message.builder().body(message.getBody()).queue(RedisMQConstant.getQueueNameByVirtual(message.getQueue()))
.tag(message.getTag()).virtualQueueName(message.getQueue()).build();
redisMQTemplate.sendMessage(build);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@PostMapping("sendTimingMessage")
public ResponseEntity sendTimingMessage(@RequestBody MQMessageDTO message) {
Object body = message.getBody();
if (body instanceof String){
String str = (String) body;
if (JsonSerializerUtil.isJson(str)){
boolean validate = JSONValidator.from(str).validate();
if (!validate) {
return ResponseEntity.ok(false);
}
}
}
String queue = message.getQueue();
Message build = Message.builder().body(message.getBody()).queue(RedisMQConstant.getQueueNameByVirtual(queue))
.tag(message.getTag()).virtualQueueName(queue).build();
redisMQTemplate.sendTimingMessage(build, message.getConsumeTime());
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
/**
* 新增队列 暂时先不接前端
*/
@PostMapping("addQueue")
public ResponseEntity addQueue(@RequestBody Queue queue) {
//队列名就是topic名
Queue queue1 = redisMQClientUtil.registerQueue(queue);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
/**
* 删除队列 暂时先不接前端
*/
@DeleteMapping("deleteQueue")
public ResponseEntity deleteQueue(String queue) {
//队列名就是topic名
Queue removeQueue = redisMQClientUtil.getQueue(queue);
redisMQClientUtil.removeQueue(removeQueue);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
public static List limitPage(List list, int start, int end) {
if (list.size() < end) {
list = list.subList(start, list.size());
} else {
list = list.subList(start, end);
}
return list;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy