All things are difficult before they are easy. 没有软件的裸机是一具僵尸,没有硬件的软件是一个幽灵。2012,专注于Linux和C语言,关注自动化、性能测试,关注开源社区和开源测试工具、方法,尝试测试团队管理!
一个让OPS调用的添加配置项的接口
上一篇 /
下一篇 2010-12-20 11:25:50
/ 个人分类:Java
ops发布当天项目,其中一些项目新增了配置项,发布后,如果我们没有及时地添加到测试共用配置项中,这样会导致测试环境编译配置时出错。为了解决这个问题,和ops讨论了一下,由我提供一个接口给它们发布当天自动调用,这个接口将当天行政的配置项传递给我们,我在接口中调用一个shell脚本将这些配置想加到我们测试配置项svn中的一个固定文件中并ci到svn库中,其他环境就可使用到了。过程中的收获:1.servlet现学现用,加深了理解;2.tomcat配置,以前主要排查问题,这次运行自己的servlet了,理解了以下其中的配置和原理;3.直接体会到jdk1.5和1.6的区别,我开始用jdk1.6写的servlet拿到jdk1.5支持的tomcat上运行,就出兼容性错误了。4.servlet调用一个shell脚本来实现添加配置项的功能,为了解决各种问题用到wget/svn up/svn ci等命令的高级用选项,深感manpage和svn up --help之类的是非常的重要,往往能发现一些帮助我解决问题的选项功能。5.最好一开始就写好log,否则查问题麻烦,最后还是得去把log写出来才方便知道哪里出错。ps:有单元测试就更好了。后续的问题:1.最好的情况下,这样是能解决问题的,但实际情况往往不是这样的,实际传过来的配置项有二方库的也有应用本身的,可能还是需要人肉去干预一下,人肉做分类整理的,否则某些环境依然有问题;不过能够一次性获得当天所有加的配置,这样也一件好事,比以前方便了点,得到的信息更全面。那个
java servlet如下(tomcat配置暂不列举)
另,由于调用方不接受我的返回值,所以那些response都时自己为了测试YY出来的。
package com.alibaba.properties;
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
/** * @className: AddProperties * @description: AddProperties * @author: 笑遍世界 * @createTime: 2010-12-9 上午10:45:18 */ public class AddProperties extends HttpServlet {
/** * version 1 */ private static final long serialVersionUID = 1L;
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InterruptedException { response.setContentType("text/html;charset=UTF-8"); PrintWriter ut = response.getWriter(); // TODO output your page here out.println("<html>"); out.println("<head>"); out.println("<title>Servlet: Jay's ServletTest</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>ServletTest at " + request.getContextPath() + "</h1>"); out.println("<h1>Request URL is " + request.getRequestURL() + "</h1>"); out.println("<h3>url parameter is " + request.getParameter("url") + "</h3>");
String url = request.getParameter("url"); String cmd = "/home/hudson/bin/addProperties.sh " + url; out.println("cmd is:"+cmd); if (url == null || url.length() < 5) { out.println("<h3>error!~~~ url is null or too short." + "</h3>"); response.setStatus(500); } else { Process process = Runtime.getRuntime().exec(cmd); int ret = process.waitFor(); out.println("return val is " + ret); if ( ret == 0 ) { out.println("addProperties.sh ok; url: " + url); response.setStatus(200); } else if ( ret == 1 ) { out.println("url is not reachable: " + url); response.setStatus(501); } else { out.println("unknown error, url: " + url); response.setStatus(555); } } out.println("<body>"); out.println(getServletInfo()); out.println("</html>"); // out.close(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { processRequest(request, response); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** */ /** * Handles the HTTP <code>POST</code> method. * * @param request * servlet request * @param response * servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { processRequest(request, response); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Returns a short description of the servlet. */ public String getServletInfo() { return "Short description"; } }
|
调用的shell脚本如下:
#!/bin/bash
if [ $# -ne 1 ]; then echo "argument count error" exit 22 fi
url=$1 addPropertiesFile="/home/hudson/commons/addProperties.txt" destFile="/home/hudson/commons/others.properties" logFile="/home/hudson/commons/url.log"
wget $url -O $addPropertiesFile -o $logFile
echo "wget url:" >> $logFile echo $url >> $logFile echo "wget --- local file:" >> $logFile echo "$addPropertiesFile" >> $logFile
mytime=$(date) echo "##------$mytime---------" >> $destFile
cd /home/hudson/commons echo "svn up:---" >> $logFile svn up --accept theirs-full >> $logFile sleep 1
cat $addPropertiesFile >> $destFile echo "svn ci:---" >> $logFile svn ci others.properties -m "add properties -- api" --username 123456 --password 123456 --no-auth-cache --non-interactive >> $logFile if [ $? -eq 0 ]; then mytime=$(date) echo "##------$mytime---ci ok------" >> $logFile exit 0; else mytime=$(date) echo "##------$mytime---------" >> $logFile echo "svn ci error.." >> $logFile exit 33 fi |
收藏
举报
TAG:
servlet
shell
Shell
svn
SVN
接口