import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.yunda.app.entity.vo.MessageQueue; import com.yunda.app.entity.vo.Page; import com.yunda.app.util.StaticVar; @Service(value = "messageQueueService") @Transactional public class MessageQueueServiceImpl implements MessageQueueService { private Scanner scanner; @Override public Page pageQuery(MessageQueue messageQueue) { Page page = new Page(); try { StringBuilder ipText = new StringBuilder(); String url = StaticVar.RABBITMQ_URL; // 用户名密码部分 String username = StaticVar.RABBITMQ_USERNAME; String password = StaticVar.RABBITMQ_PASSWORD; String input = username + ":" + password; String encoding = new String((new sun.misc.BASE64Encoder()).encode(input.getBytes())); URL myURL = new URL(url); HttpURLConnection conn = (HttpURLConnection) myURL.openConnection(); conn.setConnectTimeout(6 * 1000); conn.setReadTimeout(6 * 1000); conn.setRequestProperty("Authorization", "Basic " + encoding); //如果连接成功,获得返回字符串 if (conn.getResponseCode() == 200) { InputStream inputStream = conn.getInputStream(); scanner = new Scanner(inputStream, "utf-8"); while (scanner.hasNext()) { ipText.append(scanner.next()); } if (inputStream != null) { inputStream.close(); } conn.disconnect(); JSONArray jsonArray = JSONArray.fromObject(ipText.toString()); List<MessageQueue> mqList = new ArrayList<MessageQueue>(); int pageNo = messageQueue.getPageNo(); int pageSize = messageQueue.getPageSize(); //如果json有子项,说明有消息队列 if (jsonArray != null && jsonArray.size() > 0) { String messageQueueName = messageQueue.getName(); //不是搜索某个消息队列 if (messageQueueName == null) { //分页选取消息队列 for (int i = (pageNo - 1) * pageSize; i < pageNo * pageSize; i++) { if (i == jsonArray.size()) { break; } MessageQueue mq = new MessageQueue(); JSONObject jsonobject = jsonArray.getJSONObject(i); //获取消息队列name等字段 mq.setName(jsonobject.getString("name")); mq.setMessagesReady(jsonobject .getInt("messages_ready")); mq.setMessagesUnacknowledged(jsonobject .getInt("messages_unacknowledged")); mq.setMessages(jsonobject.getInt("messages")); mq.setConsumers(jsonobject.getInt("consumers")); mq.setState(jsonobject.getString("state")); mqList.add(mq); } if (mqList.size() > 0) { page.setTotal(jsonArray.size()); page.setRows(mqList); } } else {//是搜索某个消息队列 MessageQueue mq = new MessageQueue(); for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonobject = jsonArray.getJSONObject(i); //获取消息队列name字段,判断是否和搜索队列名称相同 String jsonobjectName=jsonobject.getString("name"); if (jsonobjectName.equals(messageQueueName)) { mq.setName(messageQueueName); mq.setMessagesReady(jsonobject .getInt("messages_ready")); mq.setMessagesUnacknowledged(jsonobject .getInt("messages_unacknowledged")); mq.setMessages(jsonobject.getInt("messages")); mq.setConsumers(jsonobject.getInt("consumers")); mq.setState(jsonobject.getString("state")); mqList.add(mq); break; } } if (mqList.size() > 0) { page.setTotal(1); page.setRows(mqList); } } } } } catch (Exception e) { e.printStackTrace(); } return page; } }
public class MessageQueue extends PageQueryParams{ //队列名称 private String name; //已准备好消息数 private int messagesReady; //未应答消息数 private int messagesUnacknowledged; //队列中消息总数 private int messages; //消费者 private int consumers; //队列状态 private String state; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMessagesReady() { return messagesReady; } public void setMessagesReady(int messagesReady) { this.messagesReady = messagesReady; } public int getMessagesUnacknowledged() { return messagesUnacknowledged; } public void setMessagesUnacknowledged(int messagesUnacknowledged) { this.messagesUnacknowledged = messagesUnacknowledged; } public int getMessages() { return messages; } public void setMessages(int messages) { this.messages = messages; } public int getConsumers() { return consumers; } public void setConsumers(int consumers) { this.consumers = consumers; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
import com.yunda.app.util.StaticVar; public class PageQueryParams { /** 分页查询的页号(前台视图页面传入) */ protected int pageNo; /** 分页查询数据时,每页显示的记录数(前台视图页面传入) */ protected int pageSize; /** 总记录数(由系统查询数据库后设置,无需过问) */ protected long total; /** 本次查询记录的开始位置(由系统查询数据库计数后设置,无需过问) */ protected long start; /**排序字段名称**/ protected String sortName; /**排序类型**/ protected String sortOrder; /** * 获取实例 * * @param pageNo * @param PageSize * @return */ public static PageQueryParams getInstance(int pageNo, int pageSize) { PageQueryParams p = new PageQueryParams(); p.setPageNo(pageNo); p.setPageSize(pageSize); return p; } public int getPageNo() { //如果页号小于等于0,则修正为1 if (pageNo <= 0) pageNo = 1; return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public long getTotal() { return total; } public void setTotal(long total) { this.total = total; } public long getStart() { return start; } public void setStart(long start) { this.start = start; } /** * 设置页号 * 提供该方法的目的是便于使用spring的自动参数注入功能 * * @param page */ public void setPage(int page) { this.pageNo = page <=0 ? StaticVar.DEFAULT_PAGE_NO : page; } /** * 设置每页显示的记录数 * 提供该方法的目的是便于使用spring的自动参数注入功能 * * @param rows */ public void setRows(int rows) { this.pageSize = rows <=0 ? StaticVar.DEFAULT_PAGE_SIZE : rows; } }