centos Percona-XtraDB-Cluster-56 安装
2015-09-24 17:45
686 查看
package util; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import javax.servlet.ServletContext; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; import com.alibaba.fastjson.JSON; import com.caucho.hessian.client.HessianProxyFactory; public class XUtil { static Logger log = Logger.getLogger(XUtil.class); static Properties prop = new Properties(); static { try { InputStream is = XUtil.class.getClassLoader().getResourceAsStream("services.properties"); prop.load(is); } catch (IOException e) { e.printStackTrace(); } } public static Object call(String sid, Object... params) { HessianProxyFactory factory = new MyHessianProxyFactory(); Object result = null; try { factory.setHessian2Reply(true); factory.setChunkedPost(false); Call caller = (Call) factory.create(Call.class, prop.getProperty("url")); result = caller.call(sid, params); } catch (Exception e) { log.debug(e.getCause().getMessage()); } return result; } public static Object call2(String sid, Object... args) { String url = "http://localhost:8080/ss/handler"; String result = null; ThreadSafeClientConnManager threadSafeClientConnManager = new ThreadSafeClientConnManager(); HttpClient client = new DefaultHttpClient(threadSafeClientConnManager); client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000); try { HttpPost post = new HttpPost(url); post.addHeader("api", sid); post.addHeader("username", ((Map<String, Object>) getUser()).get("username").toString()); System.out.println(JSON.toJSONString(args)); StringEntity s = new StringEntity(JSON.toJSONString(args), "UTF-8"); s.setContentType("application/json"); post.setEntity(s); HttpResponse res = client.execute(post); HttpEntity entity = res.getEntity(); if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { result = EntityUtils.toString(entity); } if (res.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) { throw new RuntimeException("请求未发现"); } if (res.getStatusLine().getStatusCode() == 9999) { String kk = EntityUtils.toString(entity); throw new RuntimeException(kk); } } catch (Exception e) { throw new RuntimeException(e.getMessage()); } finally { client.getConnectionManager().shutdown(); } return result; } public static void calljson(String sid, Object... args) { toJSON(call(sid, args)); } public static void call2json(String sid, Object... args) { toJSON(call2(sid, args)); } public static void toJSON(Object obj) { RequestContext.getResponse().setContentType("text/javascript;charset=UTF-8"); try { RequestContext.getResponse().getWriter().print(JSON.toJSONString(obj)); } catch (Exception e) { e.printStackTrace(); } } public static Map<String, Object> getParameterMap() { Map<String, Object> map = new HashMap<String, Object>(); Enumeration<?> em = RequestContext.getRequest().getParameterNames(); while (em.hasMoreElements()) { String k = (String) em.nextElement(); String v = RequestContext.getRequest().getParameter(k); map.put(k, v); } return map; } public static String getParameter(String name) { return RequestContext.getRequest().getParameter(name); } public static Object getUser() { return RequestContext.getSession().getAttribute("user"); } public static String getIP() { return RequestContext.getRequest().getRemoteAddr(); } @SuppressWarnings("unchecked") public static List<Map<String, Object>> toList(String str) { List<Map<String, Object>> data = null; try { data = (List<Map<String, Object>>) JSON.parseObject(str, List.class); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e.getCause()); } return data; } public static void ok() { Map<String, Object> result = new HashMap<String, Object>(); result.put("success", true); result.put("msg", null); toJSON(result); } public static void ok(String msg, Object... args) { // ResourceBundle rb = ResourceBundle.getBundle("resource", Locale.getDefault(), RequestContext.class // .getClassLoader()); // String msg = rb.getString(code); // msg = (msg != null) ? MessageFormat.format(msg, args) : code; Map<String, Object> result = new HashMap<String, Object>(); result.put("success", true); result.put("msg", msg); try { RequestContext.getResponse().getWriter().write(JSON.toJSONString(result)); } catch (Exception e) { e.printStackTrace(); } } public static void error(String msg, Object... args) { // ResourceBundle rb = ResourceBundle.getBundle("resource", Locale.getDefault(), RequestContext.class // .getClassLoader()); // String msg = rb.getString(code); // msg = (msg != null) ? MessageFormat.format(msg, args) : code; Map<String, Object> result = new HashMap<String, Object>(); result.put("success", false); result.put("msg", msg); try { RequestContext.getResponse().getWriter().write(JSON.toJSONString(result)); } catch (Exception e) { e.printStackTrace(); } } public static void attr(String key, Object value, String scope) { if ("application".equals(scope)) { RequestContext.getServletContext().setAttribute(key, value); } else if ("session".equals(scope)) { RequestContext.getSession().setAttribute(key, value); } else { RequestContext.getRequest().setAttribute(key, value); } } public static Object attr(String key, String scope) { if ("application".equals(scope)) { return RequestContext.getServletContext().getAttribute(key); } else if ("session".equals(scope)) { return RequestContext.getSession().getAttribute(key); } else { return RequestContext.getRequest().getAttribute(key); } } public static void redirect(String url) { RequestContext.redirect(url); } public static void forward(String url) { RequestContext.forward(url); } public static void invalidate() { RequestContext.invalidate(); } public static OutputStream getOutputStream() { try { return RequestContext.getResponse().getOutputStream(); } catch (IOException e) { e.printStackTrace(); } return null; } public static ServletContext getServletContext(){ return RequestContext.getServletContext(); } } interface Call { public Object call(String sid, Object... args); }
相关文章推荐
- Linux新手必须学会的12个命令
- linux添加到普通用户sudo才干
- Linux 关于Transparent Hugepages的介绍
- linux 系统启动过程
- arm-linux-gcc的总结
- 在centos卸载mysql
- unix/linux编程实践教程---sscanf函数的用法
- 自定义安装linux/Ubuntu版本操作系统/自定义磁盘分区
- centos 7 构造iptables开放80port
- zookeeper集群搭建
- centos搭建PHP的常见问题汇总
- centos crontab(定时任务) 使用
- linux下怎么查看ssh的用户登录日志
- Linux磁盘读写缓慢问题追踪
- linux环境中的arm程序编辑器eclipse安装及使用方法
- 树莓派+8187L 安装 kali linux 跑pin
- CentOS评估磁盘I/O性能读写极限测试
- 如何在 linux 中搭建 FTP 服务
- 如何选择 Linux 上的跟踪器
- centos安装poco-1.6.0-all 报错