最近项目中看到有需要在java程序中调用其他三方工具(如程序中调用三方转码工具)的需求,于是process便在这个需求中崭露头角:
(一下只简述用法,不透露实际应用)
1.调用windows平台的.bat
a.在F:盘下 新建a.txt 编辑内容为“love code”;
新建cmd.bat,编辑内容“notepad F:\a.txt”
b.在java程序中写如下调用语句
CommandUtil.exec("cmd /c start F://cmd.bat");CommandUtil的exec方法如下:
public static boolean exec(String command) throws IOException, InterruptedException{
log.info("执行脚本:"+command);
Process process = Runtime.getRuntime().exec(command);
int exitValue = process.waitFor();
if (process != null) {
process.destroy();
}
if (exitValue!=0){
return false;
}
return true;
}c.在windows平台下运行程序,变可以打开记事本,看到“love code”字样;
2.调用linux平台下的.sh脚本
用法一样,只是把bat文件换成linux下的shell脚本文件,并用相同代码调用 。
在linux平台下运行程序即可调用shell脚本。
原理:设置 web.xml,使 Servlet 在项目启动时自动创建对象,各用户访问 Servlet 可实现在 Servlet 下的对象共享,再通过 Session 可将 JSP 也实现对象共享。
将要共享的对象 ShareInfo.java
/**
*
*/
package test;
/**
* @author Administrator
*
*/
public class ShareInfo
{
private int val = 0;
/**
*
*/
public ShareInfo()
{
// TODO Auto-generated constructor stub
}
/**
* 累加
*/
public void add()
{
val = val + 1;
}
/**
* 取值
*
* @return
*/
public int getVal()
{
return val;
}
}
启动时加载的 Servlet
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import test.ShareInfo;
/**
* Servlet implementation class Info
*/
@WebServlet(name = "info", urlPatterns = { "/info" })
public class Info extends HttpServlet
{
private static final long serialVersionUID = 1L;
// 声明共享对象
private ShareInfo shareInfo = null;
/**
* @see HttpServlet#HttpServlet()
*/
public Info()
{
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException
{
// TODO Auto-generated method stub
// 创建共享对象
shareInfo = new ShareInfo();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
// 对象得到共享
// 对象内数字累加
shareInfo.add();
// 存入 session,jsp 页面也将获得共享
HttpSession httpSession = request.getSession();
httpSession.setAttribute("INFO", shareInfo);
processRequest(String.valueOf(shareInfo.getVal()), request, response);
}
/**
* return text to explorer
*
* @param strMessage
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void processRequest(String strMessage, HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
out.print(strMessage);
}
finally
{
out.close();
}
}
}
设置启动 Servlet 的 web.xml
<servlet> <servlet-name>Info</servlet-name> <servlet-class>servlet.Info</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
获得共享的 jsp
<%@page import="test.ShareInfo"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="test.ShareInfo"%>
<%
ShareInfo shareInfo = null;
Object object = session.getAttribute("INFO");
if (object != null)
{
shareInfo = (ShareInfo) object;
shareInfo.add();
}
else
{
out.print("没有找到对象,用户需要访问一次 servlet 获取 session 赋值");
return;
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>GlassFish JSP Page</title>
</head>
<body>
<%=shareInfo.getVal()%>
</body>
</html>
源码下载
http://download.csdn.net/detail/joyous/5345508
Q群讨论:236201801
以下几篇文章是较深入分析binder机制。
目录
1. Android - Binder机制 - ServiceManager
2. Android - Binder机制 - 普通service注册
3. Android - Binder机制 - 获得普通service
4. Android - Binder机制 - client和普通service交互
5. Android - Binder机制 - Binder框架总结
6. Android - Binder机制 - ProcessState和IPCThreadState
7. Android - Binder机制 - 驱动
UML
1. 以中间的IXXX的垂直线为准,左边是客户端进程,它们的命名类似Bp***,右边是服务端进程,它们的命名类似Bn***;
2. 以中间的一条水平虚线为界线,上边执行的是具体业务,如我们之前讲到的AddServcie、GetService、StartPreview等,它们都是普通业务,下边执行的是数据交互,就是讲上边的业务数据打包成binder定义的数据包结构,然后通过binder驱动发送出去或者接收;
3. 第一篇的ServiceManager不是按照Bn***类构建的,但是只是我用的版本不是这样构建的,ServiceManager也完全可以用Bn***来构建,用Bp***和Bn***来构建,让程序员在看代码时更加轻松,代码结构也更加简洁,所以,基本上都是通过Bp***和Bn***来完成的;
4. Bn***和Bp***都继承了两个基类,一个是IXXX,一个是BBinder(或者BpRefBase),其实也正说明了BpXXX和BnXXX既要完成业务层的任务,也要执行数据传输相应的任务;
5. IPCThreadState是真正和驱动打交道的角色,一个进程可以有几个;
6. ProcessState的任务很简单,一是打开binder设备供IPCThreadState使用,一个是获得ServiceManager;
7. 客户端至少持有两个服务端,一个是ServiceManager,一个是它的业务服务端XXXService;
通过这个图,你是不是对复杂的binder找到了很多的规律了;