首页 > 解决方案 > Thread Busy apache tomcat 解压数据

问题描述

我已经在 tomcat 中部署了应用程序,并且有很多线程很忙,没有像这样发布超过 700 个线程。

我捕获了文件在ufile.io/8zz1t上的 thead 转储,并且我使用fastthread.io来读取。你能检查一下你是否看到了问题,我看到充气机有线程消耗 CPU。

S   188063346 ms    0 KB    0 KB    10.162.3.36 172.30.100.163  POST /ChiperService/rest/cs/Descifrar HTTP/1.1
S   280064346 ms    0 KB    0 KB    10.162.3.36 172.30.100.163  POST /ChiperService/rest/cs/Descifrar HTTP/1.1
S   185431144 ms    0 KB    0 KB    10.162.38.201   172.30.100.163  POST /ChiperService/rest/cs/Descifrar HTTP/1.1
S   267094596 ms    0 KB    0 KB    10.162.3.36 172.30.100.163  POST /ChiperService/rest/cs/Descifrar HTTP/1.1
S   261396699 ms    0 KB    0 KB    10.162.3.36 172.30.100.163  POST /ChiperService/rest/cs/Descifrar HTTP/1.1

这段代码的哪一部分会导致线程忙?我不知道放气机或充气机是否必须靠近。

在应用程序 ChiperService 的 tomcat 管理器中,没有活动会话。

请帮助服务器在一天中几乎崩溃了 5 次,因为头很忙并且 CPU 消耗很高。

这是休息服务:

package ChiperServicePkg;

import com.sun.jersey.api.core.ResourceConfig;
import java.io.IOException;
import javax.ws.rs.core.Context;
import javax.ws.rs.POST;
import javax.ws.rs.PathParam;
import javax.ws.rs.Path;

import javax.ws.rs.core.Response;
import principal.allus.com.co.SBCCypherModuleMain;
/**
 * REST Web Service
 *
 * @author 1017200731
 */
@Path("/cs")
public class CiphersResource {

@Context ResourceConfig Config;

/**
 * Creates a new instance of CiphersResource
 */
public CiphersResource() {
}

/**
 *
 * @param UUI
 * @return
 * @throws Exception
 */
@POST
@Path("Cifrar")    
public String Cifrar(String UUI) throws Exception 
{
    String Key = (String) Config.getProperty("KeyCipher");
    String dataEncrypted = null;
    try
    {
        dataEncrypted= SBCCypherModuleMain.cifrar(UUI,Key );            
    }
    catch(Exception ex)
    {
        if (ex instanceof IOException){
            throw new IOException(ex);
        }
        else{
            throw ex;
        }            
    }
    return dataEncrypted;
}

/**
 *
 * @param dataEncrypted
 * @return
 * @throws Exception
 */
@POST
@Path("Descifrar")
public Response Descifrar(String dataEncrypted) throws Exception
{
    String Key = (String) Config.getProperty("KeyCipher");
    String dataDecrypted= "";
    
    try
    {
        dataDecrypted= SBCCypherModuleMain.descifrar(dataEncrypted, Key);
    }
    catch(Exception ex)
    {            
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
            .entity(ex.getMessage()).type("text/plain").build();
    }
    return Response.ok(dataDecrypted).build();
}  

/**
 * Sub-resource locator method for {id}
 */
@Path("{id}")
public CipherResource getCipherResource(@PathParam("id") String id) {
    return CipherResource.getInstance(id);
}       

}

Descifrar 方法调用客户端提供的 jar 并使用反编译器可以提取以下代码:

  public static String descifrar(String bytes, String llave)
    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, Exception
  {
    byte[] vector = null;
    String retorno = "";
    
    retorno = SBCCypherModuleCompress.descomprimir(SBCCypherModuleCypher.descifrar(bytes, llave.substring(0, 16)));
    
    return retorno;
  }

SBCCypherModuleCompress 类如下:

 public class SBCCypherModuleCompress
{
  public static String comprimir(byte[] data)
    throws IOException, Exception
  {
BASE64Encoder b64e = new BASE64Encoder();
    
    byte[] output = null;
    String salida = "";
    
    Deflater deflater = new Deflater();
    deflater.setInput(data);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    deflater.finish();
    
    byte[] buffer = new byte['?'];
    while (!deflater.finished())
    {
      int count = deflater.deflate(buffer);
      outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    output = outputStream.toByteArray();
    
    salida = b64e.encode(output);
    
    return salida;
  }
  
  public static String descomprimir(String data)
    throws DataFormatException, IOException, Exception
  {
    BASE64Encoder b64e = new BASE64Encoder();
    BASE64Decoder b64d = new BASE64Decoder();
    
    byte[] output = null;
    String salida = "";
    byte[] datad = null;
    
    datad = b64d.decodeBuffer(data);
    
    Inflater inflater = new Inflater();
    
    inflater.setInput(datad);
    
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(datad.length);
    
    byte[] buffer = new byte['?'];
    while (!inflater.finished())
    {
      int count = inflater.inflate(buffer);
      outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    
    output = outputStream.toByteArray();
    
    salida = b64e.encode(output);
    
    return new String(output);
  }
}

SBCCypherModuleCypher 类如下:

public class SBCCypherModuleCypher
{
  public static String cifrar(String vector, String llaveSimetrica)
    throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, Exception
  {
    BASE64Encoder b64e = new BASE64Encoder();
    BASE64Decoder b64d = new BASE64Decoder();
    
    byte[] datad = null;
    String salida = "";
    
    datad = b64d.decodeBuffer(vector);
    
    SecretKeySpec key = new SecretKeySpec(llaveSimetrica.getBytes(), "AES");
    
    byte[] campoCifrado = null;
    
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(1, key);
    campoCifrado = cipher.doFinal(datad);
    
    salida = b64e.encode(campoCifrado);
    
    return salida;
  }
  
  public static String descifrar(String vector, String llaveSimetrica)
    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, Exception
  {
    BASE64Encoder b64e = new BASE64Encoder();
    BASE64Decoder b64d = new BASE64Decoder();
    
    byte[] datad = null;
    String salida = "";
    
    datad = b64d.decodeBuffer(vector);
    
    SecretKeySpec key = new SecretKeySpec(llaveSimetrica.getBytes(), "AES");
    
    Cipher cipher = Cipher.getInstance("AES");
    
    cipher.init(2, key);
    
    byte[] datosDecifrados = cipher.doFinal(datad);
    
    salida = b64e.encode(datosDecifrados);
    
    return salida;
  }
}

标签: javaresttomcat

解决方案


你所有的线程都inflater.inflate(buffer)在 SBCCypherModuleCompress.java 中,第 92 行。

但是你为什么写new byte['?']?这是在以下代码中:

byte[] buffer = new byte['?'];
while (!deflater.finished())
{
  int count = deflater.deflate(buffer);
  outputStream.write(buffer, 0, count);
}

以及以下代码:

byte[] buffer = new byte['?'];
while (!inflater.finished())
{
  int count = inflater.inflate(buffer);
  outputStream.write(buffer, 0, count);
}

new byte['?']没有意义。

new byte[2048]或者也许new byte[8192]会更好,而不是new byte['?'].


推荐阅读