分类 Java 下的文章

connectionTimeout:连接超时时间(单位:毫秒,默认值:60000),tomcat接受连接后到客户端首次发送数据的最大等待时间,如果超过此时间,tomcat将会主动关闭连接
keepAliveTimeout:长连接最大空闲时间(单位:毫秒,默认值:等于connectionTimeout),表示长连接两次请求之间最大等待时间,该长连接最后一次请求后等待此时间仍然没有接收到另一个请求,tomcat将会主动关闭连接
maxKeepAliveRequests:长连接最大请求数(默认值:100,1表示禁用长连接,-1表示不限制),该长连接请求次数达到此数值后,tomcat将会主动关闭连接

- 阅读剩余部分 -

mvn clean package -pl bioframe-api -am -Dmaven.test.skip=true

参数说明:
-am --also-make 同时构建所列模块的依赖模块;
-amd -also-make-dependents 同时构建依赖于所列模块的模块;
-pl --projects <arg> 构建制定的模块,模块间用逗号分隔;
-rf -resume-from <arg> 在完整的反应堆构建顺序基础上,指定从哪个模块开始构建;

mvn deploy:deploy-file -Durl=http://nexus.xxx.com/repository/3rd-party/ -DrepositoryId=nexus -DgroupId=3rd-party -DartifactId=com.sun.jna -Dversion=4.1.0 -Dpackaging=jar -Dfile=./com.sun.jna_4.1.0.v20170410-1117.jar

public static class IntSumReducer extends Reducer<Text, Text, Text, IntWritable> {
        private List<Text> list=null;
        
        @Override
        protected void setup(Reducer<Text, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            list=new ArrayList<Text>();
        }
        
        @Override
        public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            for (Text value : values) {
                //错误的写法,reduce的key和value分别共享同一对象,每次迭代的时候,只是更新了这个共享对象的值,如果直接引用此对象,该list中所有的对象的值都会变成最后一次迭代的value的值
                list.add(value);
                //正确的写法,应该将value复制出一个新的对象,key同理
                list.add(new Text(value));
            }
        }
        
        @Override
        protected void cleanup(Reducer<Text, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            for(Text obj:list){
                context.write(obj, new IntWritable(1));
            }
        }
}

从jdk7开始增加了try-with-resources模式,可以自动关闭资源,代码示例如下:

try (OutputStream out = openOutputStream(file, append)) {
IOUtils.write(data, out, encoding);
}
System.out.println("continue");

try()中定义的变量(必须实现AutoCloseable接口)在{}代码执行结束(包含异常退出)后,会自动调用变量的close()方法。

- 阅读剩余部分 -

JDK自带的HttpURLConnection,默认启用keepAlive,支持HTTP / 1.1和HTTP / 1.0持久连接, 使用后的HttpURLConnection会放入缓存中供以后的同host:port的请求重用,底层的socket在keepAlive超时之前不会关闭。

HttpURLConnection受以下system properties控制:
http.keepAlive=<boolean>(默认值:true),是否启用keepAlive,如果设置为false,则HttpURLConnection不会缓存,使用完后会关闭socket连接。
http.maxConnections=<int>(默认值:5),每个目标host缓存socket连接的最大数。

- 阅读剩余部分 -

servlet、filter、listener分别定义2个,代码如下:

public class GlobalServlet extends HttpServlet {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void init(ServletConfig config) throws ServletException {
        logger.info("init");
    }
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        logger.info("service");
        PrintWriter pw = resp.getWriter();
        pw.write("This is "+this.getClass().getSimpleName());
        pw.close();
    }
    
    @Override
    public void destroy() {
        logger.info("destroy");
    }
}

public class LoginServlet extends HttpServlet {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void init(ServletConfig config) throws ServletException {
        logger.info("init");
    }
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        logger.info("service");
        PrintWriter pw = resp.getWriter();
        pw.write("This is "+this.getClass().getSimpleName());
        pw.close();
    }
    
    @Override
    public void destroy() {
        logger.info("destroy");
    }
}

public class GlobalFilter implements Filter {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        logger.info("start doFilter");
        chain.doFilter(request, response);
        logger.info("end doFilter");
    }

    @Override
    public void destroy() {
        logger.info("destroy");
    }
}

public class LoginFilter implements Filter {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        logger.info("start doFilter");
        chain.doFilter(request, response);
        logger.info("end doFilter");
    }

    @Override
    public void destroy() {
        logger.info("destroy");
    }
}

public class ContextListener1 implements ServletContextListener {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        logger.info("contextInitialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        logger.info("contextDestroyed");
    }
}

public class ContextListener2 implements ServletContextListener {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        logger.info("contextInitialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        logger.info("contextDestroyed");
    }
}

- 阅读剩余部分 -