分类归档:Web程序开发

Spring Async 实现 ForkJoin

第一种情况,Service类:

@Async
public String test1() {
    String threadName = Thread.currentThread().getName();
    log.error("{}: 1 start", threadName);
    try {
        Thread.sleep(5000L);
    } catch (Exception e){
    }
    log.error("{}: 1 end", threadName);
    return "s1";
}

@Async
public String test2() {
    String threadName = Thread.currentThread().getName();
    log.error("{}: 2 start", threadName);
    try {
        Thread.sleep(2000L);
    } catch (Exception e){
    }
    log.error("{}: 2 end", threadName);
    return "s2";
}

Controller类:

try {
    String threadName = Thread.currentThread().getName();
    log.error("{}: 0 start", threadName);
    PrintWriter out = response.getWriter();
    String f1 = testService.test1();
read more

linux bugzilla nginx 安装配置 详解

Bugzilla是Mozilla公司提供的一款开源的免费Bug追踪系统,用来帮助你管理软件开发,建立完善的BUG跟踪体系。

在网上看了一下,bugzilla的安装,大部分的web环境是apache 。下面说一下我的安装过程。

一,安装epel源

请参考:centos 推荐使用epel源

二,安装mysql nginx bugzilla,以及相关工具

  1. yum install mysql mysql-server bugzilla fcgi-devel fcgi nginx perl spawn-fcgi

安装fcgiwrap,它是用来运行perl的cgi程序的

fcgiwrap下载地址: http://github.com/gnosek/fcgiwrap/tarball/master

  1. [root@localhost download]# tar zxvf gnosek-fcgiwrap-1.1.0-5-g66e7b7d.tar.gz
  2. [root@localhost download]# cd gnosek-fcgiwrap-66e7b7d/
  3. [root@localhost gnosek-fcgiwrap-66e7b7d]# autoreconf -i
  4. [root@localhost gnosek-fcgiwrap-66e7b7d]# ./configure
  5. [root@localhost gnosek-fcgiwrap-66e7b7d]# make
  6. gcc -std=gnu99 -Wall -Wextra -Werror -pedantic -O2 -g3 fcgiwrap.c -o fcgiwrap -lfcgi
  7. [root@localhost gnosek-fcgiwrap-66e7b7d]# cp fcgiwrap /usr/local/bin/

如果autoreconf -i时,autoreconf命令不存在,安装如下依赖包:

  1. yum install patch gcc-c++ readline-devel zlib-devel libffi-devel openssl-devel make autoconf automake libtool bison libxml2-devel libxslt-devel libyaml-devel

三,启动mysql,并创建bugzilla数据库

  1. /etc/init.d/mysqld start   //启动服务端
  2. mysql> create database bugstest;    //创建空数据库
  3. mysql> grant all privileges on bugstest.* TO ‘bugstest’@‘localhost’ IDENTIFIED BY ‘bugstest’;   //分配权限
  4. mysql> flush privileges;

四,修改bugzilla的配置localconfig,并且安装bugzilla数据库

1,修改/etc/localconfig

  1. $db_name = ‘bugstest’;      //数据库名
  2. $db_user = ‘bugstest’;      //连接用户名
  3. $db_pass = ‘bugstest’;        //连接密码

就是上面,mysql里面添加的用户。

read more

java中RSA加解密的实现

public static void main(String[] args) throws Exception {  
        // TODO Auto-generated method stub  
        HashMap<String, Object> map = RSAUtils.getKeys();  
        //生成公钥和私钥  
        RSAPublicKey publicKey = (RSAPublicKey) map.get("public");  
        RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");  
          
        //模  
        String modulus = publicKey.getModulus().toString();  
        //公钥指数  
        String public_exponent = publicKey.getPublicExponent().toString();  
        //私钥指数  
        String private_exponent = privateKey.getPrivateExponent().toString();  
        //明文  
        String ming = "123456789";  
        //使用模和指数生成公钥和私钥
read more

hibernate用hbm2java给pojo增加serialVersionUID

Hibernate 3.2.x 工具中hbm2java生成的pojo类总是类似下面:

public class Child implements java.io.Serializable {
 private int cid;
 private String childName;
 public int getCid() {
 return this.cid;
 }
 public void setCid(int cid) {
 this.cid = cid;
 }
 public String getChildName() {
 return this.childName;
 } 
 public void setChildName(String childName) {
 this.childName = childName;
 }
}

但是如果你使用JDK5.0或更高版本时,总是警告该类缺少个static final long serialVersionUID,如何才能在hbm2java生成java源代码时自动加上呢?这就要修改hibernate-tools.jar中自带的pojo模板了。

首先,你将hibernate-toosl.jar中的pojo/Pojo.ftl文件解压出来,在<#if !pojo.isInterface()>的下一行增加:static final long serialVersionUID = 1L;增加后整个Pojo.ftl文件内容应该是这样:

${pojo.getPackageDeclaration()}
// Generated ${date} by Hibernate Tools ${version}
<#assign classbody>
<#include "PojoTypeDeclaration.ftl"/> {
<#if !pojo.isInterface()>
static final long serialVersionUID = 1L;
<#include "PojoFields.ftl"/>
<#include "PojoConstructors.ftl"/>
 
<#include "PojoPropertyAccessors.ftl"/>
<#include
read more

生产环境下JAVA进程高CPU占用故障排查

问题描述:
生产环境下的某台tomcat7服务器,在刚发布时的时候一切都很正常,在运行一段时间后就出现CPU占用很高的问题,基本上是负载一天比一天高。

问题分析:
1,程序属于CPU密集型,和开发沟通过,排除此类情况。
2,程序代码有问题,出现死循环,可能性极大。

问题解决:
1,开发那边无法排查代码某个模块有问题,从日志上也无法分析得出。
2,记得原来通过strace跟踪的方法解决了一台PHP服务器CPU占用高的问题,但是通过这种方法无效,经过google搜索,发现可以通过下面的方法进行解决,那就尝试下吧。

解决过程:
1,根据top命令,发现PID为2633的Java进程占用CPU高达300%,出现故障。

2,找到该进程后,如何定位具体线程或代码呢,首先显示线程列表,并按照CPU占用高的线程排序:
read more

HTML页面内容禁止选择、复制、右键

<body leftmargin=0 topmargin=0  oncontextmenu=’return false’ ondragstart=’return false’ onselectstart =’return false’ onselect=’document.selection.empty()’ oncopy=’document.selection.empty()’ onbeforecopy=’return false’ onmouseup=’document.selection.empty()’>  


oncontextmenu='return false'
ondragstart='return false'
onselectstart ='return false'
onselect='document.selection.empty()'
oncopy='document.selection.empty()'
onbeforecopy='return false'
onmouseup='document.selection.empty()'

PHP 5.5.15安装intl模块

给PHP环境安装intl模块需要安装 icu4c。

intl模块安装

安装icu4c在本地环境。

wget http://download.icu-project.org/files/icu4c/53.1/icu4c-53_1-src.tgz
tar -xzf icu4c-53_1-src.tgz
cd icu/source
./configure –prefix=/usr/local/icu
make
make install

从Pecl下载intl模块

wget http://pecl.php.net/get/intl-3.0.0.tgz
tar -xzf intl-3.0.0.tgz
cd intl-3.0.0

/usr/local/php/bin/phpize
./configure –enable-intl –with-icu-dir=/usr/local/icu/ –with-php-config=/usr/local/php/bin/php-config
make
make install

配置php.ini启用intl模块

sed -i ‘s#; extension_dir = \”\.\/\”#extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/”#’ /etc/php.ini
echo “;extension=intl.so” >> /etc/php.ini

Symfony2学习笔记之事件分配器

—-EventDispatcher组件使用

简介:

面向对象编程已经在确保代码的可扩展性方面走过了很长一段路。它是通过创建一些责任明确的类,让它们之间变得更加灵活,开发者可以通过继承这些类创建子类,来改变它们的行为。但是如果想将某个开发者的改变跟其它已经编写了自己子类的开发者共享,这种面向对象的继承就不再那么有用了。

举一个现实的实例,你想为你的项目提供一个插件系统。一个能够被添加到方法的插件,或者在方法执行的前后完成某些工作,而不会干扰到其它插件。这个通过单一的继承完成不是一个容易的事情,多重继承又有它的局限性。

read more

Symfony2学习笔记之数据校验

校验在web应用程序中是一个常见的任务。数据输入到表单需要被校验。数据在被写入数据库之前或者传入一个webservice时也需要被校验。

Symfony2 配备了一个Validator 组件,它让校验工作变得简单易懂。该组件是基于JSR303 Bean校验规范。一个Java规范用在PHP中。

基本验证
理解校验的最好方法是看它的表现。首先,假设你已经创建了一个用于你应用程序某个地方的PHP对象。
//src/Acme/BlogBundle/Entity/Author.php
namespace Acme\BlogBundle\Entity;

class Author
{
public $name;
}

到现在为止,它只是个服务于你应用程序的某些目的的普通的类。而校验的目的就是要告诉你对象的数据是否合法。为了这个目的,你需要配置一个对象必须遵守规则或者约束列表来让自己的数据合法。这些规则可以被描述成多种不同的格式的(比如,YAML,XML,类声明或者PHP)。比如,我们保证属性$name不能为空,来添加下面的规则:
read more

Symfony2学习笔记之HTTP Cache

富web应用程序的本质意味着它们的动态。无论你的应用程序多么有效率,每个请求比起静态文件来说总会存在很多的耗费。对于大多数web程序来说,这没什么。 Symfony2非常的轻快,无论你做些严重超载的请求,每个请求将会得到很快的回复,而不会对你的服务器造成压力。但是随着你站点的成长,负载将成为一个严重的问题。对每个请求处理应该只被正常执行一次。这就是缓存真正要达成的目标。
站在巨人肩膀上的缓存:
提高一个应用程序执行效率的最有效方法是缓存一个页面的所有输出然后让后续的请求绕开整个应用程序。当然,这对于高动态性的站点来说并不是总是可能的。Symfony2 缓存系统是比较特别的,因为它依赖于在HTTP规范中定义的简单强大的HTTP read more