java响应很慢排插_服务响应时间慢:Java SecureRandom和/ dev / random - java
我正在嘗試調(diào)試Tomcat上部署的應用程序提供的一些慢速響應。
現(xiàn)在,我主要關(guān)注SecureRandom和/dev/random(其他一些可能的原因已被調(diào)查并排除)。
模式如下:
第一個調(diào)用恰好在Tomcat重新啟動后(即使請求在啟動后4分鐘到達)也僅需30.0 xy秒
稍后,某些調(diào)用恰好需要15.0 pq秒(沒有可以建立的特定模式,pq是TP99中花費的時間)。
服務調(diào)用涉及加密和解密(AES / ECB / PKCS5Padding)。
SecureRandom初始化/重新填充是否有可能導致這種情況?
(盡管在catalina.log中寫了一個日志,上面寫著"Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [28,760] milliseconds.")
另外,為了檢查是否正在使用/dev/random或/dev/urandom,我使用了this question的測試。令我驚訝的是,與鏈接問題中發(fā)生的情況不同,我沒有看到它們中的任何一個。
這些是strace日志的最后幾行:
3561 lstat("/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/jsse.jar", {st_mode=S_IFREG|0644, st_size=258525, ...}) = 0
3561 open("/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/jsse.jar", O_RDONLY) = 6
3561 stat("/dev/random", {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 8), ...}) = 0
3561 stat("/dev/urandom", {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 9), ...}) = 0
3561 open("/dev/random", O_RDONLY) = 7
3561 open("/dev/urandom", O_RDONLY) = 8
3561 unlink("/tmp/hsperfdata_xxxx/3560") = 0
那么,什么用于播種SecureRandom?
fyi,java -version
java version "1.6.0_32"
OpenJDK Runtime Environment (IcedTea6 1.13.4) (rhel-7.1.13.4.el6_5-x86_64)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)
參考方案
我無法檢查您的OpenJDK具體版本,但可以檢查jdk6-b33。
SecureRandom使用SeedGenerator獲取種子字節(jié)
public byte[] engineGenerateSeed(int numBytes) {
byte[] b = new byte[numBytes];
SeedGenerator.generateSeed(b);
return b;
}
SeedGenerator從SunEntries獲取seedSource(字符串)
String egdSource = SunEntries.getSeedSource();
SunEntries嘗試首先從系統(tǒng)屬性java.security.egd獲取源,如果找不到,則嘗試從securerandom.source屬性文件獲取java.security屬性,如果找不到該屬性,則返回空白字符串。
// name of the *System* property, takes precedence over PROP_RNDSOURCE
private final static String PROP_EGD = "java.security.egd";
// name of the *Security* property
private final static String PROP_RNDSOURCE = "securerandom.source";
final static String URL_DEV_RANDOM = "file:/dev/random";
final static String URL_DEV_URANDOM = "file:/dev/urandom";
private static final String seedSource;
static {
seedSource = AccessController.doPrivileged(
new PrivilegedAction() {
public String run() {
String egdSource = System.getProperty(PROP_EGD, "");
if (egdSource.length() != 0) {
return egdSource;
}
egdSource = Security.getProperty(PROP_RNDSOURCE);
if (egdSource == null) {
return "";
}
return egdSource;
}
});
}
SeedGenerator檢查此值以初始化實例
// Static instance is created at link time
private static SeedGenerator instance;
private static final Debug debug = Debug.getInstance("provider");
final static String URL_DEV_RANDOM = SunEntries.URL_DEV_RANDOM;
final static String URL_DEV_URANDOM = SunEntries.URL_DEV_URANDOM;
// Static initializer to hook in selected or best performing generator
static {
String egdSource = SunEntries.getSeedSource();
// Try the URL specifying the source
// e.g. file:/dev/random
//
// The URL file:/dev/random or file:/dev/urandom is used to indicate
// the SeedGenerator using OS support, if available.
// On Windows, the causes MS CryptoAPI to be used.
// On Solaris and Linux, this is the identical to using
// URLSeedGenerator to read from /dev/random
if (egdSource.equals(URL_DEV_RANDOM) || egdSource.equals(URL_DEV_URANDOM)) {
try {
instance = new NativeSeedGenerator();
if (debug != null) {
debug.println("Using operating system seed generator");
}
} catch (IOException e) {
if (debug != null) {
debug.println("Failed to use operating system seed "
+ "generator: " + e.toString());
}
}
} else if (egdSource.length() != 0) {
try {
instance = new URLSeedGenerator(egdSource);
if (debug != null) {
debug.println("Using URL seed generator reading from "
+ egdSource);
}
} catch (IOException e) {
if (debug != null)
debug.println("Failed to create seed generator with "
+ egdSource + ": " + e.toString());
}
}
// Fall back to ThreadedSeedGenerator
if (instance == null) {
if (debug != null) {
debug.println("Using default threaded seed generator");
}
instance = new ThreadedSeedGenerator();
}
}
如果來源是
final static String URL_DEV_RANDOM = "file:/dev/random";
要么
final static String URL_DEV_URANDOM = "file:/dev/urandom"
在Windows上使用NativeSeedGenerator嘗試在Linux上使用本機CryptoAPI該類只是擴展了SeedGenerator.URLSeedGenerator
package sun.security.provider;
import java.io.IOException;
/**
* Native seed generator for Unix systems. Inherit everything from
* URLSeedGenerator.
*
*/
class NativeSeedGenerator extends SeedGenerator.URLSeedGenerator {
NativeSeedGenerator() throws IOException {
super();
}
}
并調(diào)用默認情況下加載/dev/random的超類構(gòu)造函數(shù)
URLSeedGenerator() throws IOException {
this(SeedGenerator.URL_DEV_RANDOM);
}
因此,在沒有在系統(tǒng)屬性/dev/random或安全屬性文件的java.security.egd屬性中未設置其他值之前,OpenJDK默認使用securerandom.source。
如果要使用strace查看讀取結(jié)果,可以更改命令行并添加trace=open,read表達式
sudo strace -o a.strace -f -e trace=open,read java class
您會看到類似的內(nèi)容(我使用Oracle JDK 6進行了測試)
13225 open("/dev/random", O_RDONLY) = 8
13225 read(8, "@", 1) = 1
13225 read(3, "PK\3\4\n\0\0\0\0\0RyzB\36\320\267\325u\4\0\0u\4\0\0 \0\0\0", 30) = 30
....
....
如果您在啟動過程中遇到延遲,請參閱“Tomcat Wiki”部分中有關(guān)加快啟動速度的建議,使用/ dev / urandom之類的非阻塞熵源
更多信息:https://wiki.apache.org/tomcat/HowTo/FasterStartUp#Entropy_Source
希望這可以幫助。
Java-搜索字符串數(shù)組中的字符串 - java
在Java中,我們是否有任何方法可以發(fā)現(xiàn)特定字符串是字符串數(shù)組的一部分。我可以避免出現(xiàn)一個循環(huán)。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一個if (some condition to tell wheth…產(chǎn)生6位數(shù)的隨機數(shù) - java
我只想生成6位數(shù)的隨機數(shù),范圍應從000000到999999。new Random().nextInt(999999)返回我的電話號碼,但不是6位數(shù)字。 參考方案 就這么簡單,您可以使用您的代碼,并在此處做一件事String.format("%06d", number); 這將以字符串格式返回您的數(shù)字,因此“0”將為“000000”。這是…Java Scanner讀取文件的奇怪行為 - java
因此,在使用Scanner類從文件讀取內(nèi)容時,我遇到了一個有趣的問題?;旧?#xff0c;我試圖從目錄中讀取解析應用程序生成的多個輸出文件,以計算一些準確性指標?;旧?#xff0c;我的代碼只是遍歷目錄中的每個文件,并使用掃描儀將其打開以處理內(nèi)容。無論出于何種原因,掃描程序都不會讀取其中的一些文件(所有UTF-8編碼)。即使文件不是空的,scanner.hasNextLine()在…Java Globbing模式以匹配目錄和文件 - java
我正在使用遞歸函數(shù)遍歷根目錄下的文件。我只想提取*.txt文件,但不想排除目錄?,F(xiàn)在,我的代碼如下所示:val stream = Files.newDirectoryStream(head, "*.txt") 但是這樣做將不會匹配任何目錄,并且返回的iterator()是False。我使用的是Mac,所以我不想包含的噪音文件是.DS_ST…直接讀取Zip文件中的文件-Java - java
我的情況是我有一個包含一些文件(txt,png,...)的zip文件,我想直接按它們的名稱讀取它,我已經(jīng)測試了以下代碼,但沒有結(jié)果(NullPointerExcepion):InputStream in = Main.class.getResourceAsStream("/resouces/zipfile/test.txt"); Buff…
總結(jié)
以上是生活随笔為你收集整理的java响应很慢排插_服务响应时间慢:Java SecureRandom和/ dev / random - java的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: adb—fastboot—Downloa
- 下一篇: 计算机音乐apple,Apple Mus