package com.yunda.app.service; 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; } }
package com.yunda.app.entity.vo; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * 分页信息实体 * * @author JS * @since 2013-04-09 */ @SuppressWarnings("serial") public class Page implements Serializable { /**总记录数*/ private long total; /**当前页记录,类型为List*/ private Object rows; /**列表底部显示的数据,例如统计数据,类型为List*/ private Object footer; /** * 构造方法,只构造空页. */ public Page() { this(0, new ArrayList()); } /** * 不分页列表数据对象默认构造方法 * * @param rows 列表数据 */ public Page(Object rows) { this.rows = rows; if(null != rows && rows instanceof List) { this.total = ((List)rows).size(); } else if (null != rows){ this.total = 1; } } /** * 分页列表数据对象默认构造方法(仅含含列表) * * @param rows 列表数据 */ public Page(long total, Object rows) { this.total = total; this.rows = rows; } /** * 默认分页构造方法(含列表和表底) * * @param total 总记录数 * @param rows 页数据 * @param footer 页脚数据 */ public Page(long total, Object rows, Object footer) { this.total = total; this.rows = rows; this.footer = footer; } /** * 取当前页中的记录. */ public Object getRows() { return rows; } /** * 设置当前页中的记录. */ public void setRows(Object rows){ this.rows = rows; } /** * 获取总记录数 * * @return */ public long getTotal() { return total; } /** * 设置总记录数 * * @param total */ public void setTotal(long total) { this.total = total; } public Object getFooter() { return footer; } public void setFooter(Object footer) { this.footer = footer; } /** * 获取任一页第一条数据在数据集的位置(不考虑总记录数) * * @param pageNo 从1开始的页号 * @param pageSize 每页记录条数 * @return 该页第一条数据 */ public static long getStartOfPage(int pageNo, int pageSize) { long start = (pageNo - 1) * pageSize; return start < 0 ? 0 : start; } /** * 获取任一页第一条数据在数据集的位置(考虑总记录数并做调整) * * @param total 总记录数 * @param pageNo 从1开始的页号 * @param pageSize 每页记录条数 * @return 该页第一条数据 */ public static long getStartOfPage(long total, int pageNo, int pageSize) { long start = (pageNo - 1) * pageSize; //如果超过了最后一行,则自动退回到最后一页的第一行 if (start >= total) { start = Page.getStartOfPage(pageNo-1, pageSize); } return start < 0 ? 0 : start; } }