分类归档:Java

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

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