我正在重构一些使用番石榴缓存的代码.
初始代码:
public Post getPost(Integer key) throws SQLException, IOException {
return PostsDB.findPostByID(key);
}
Run Code Online (Sandbox Code Playgroud)
为了不破坏某些东西,我需要保留任何抛出的异常,而不包装它.
目前的解决方案看起来有些难看
public Post getPost(final Integer key) throws SQLException, IOException {
try {
return cache.get(key, new Callable<Post>() {
@Override
public Post call() throws Exception {
return PostsDB.findPostByID(key);
}
});
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof SQLException) {
throw (SQLException) cause;
} else if (cause instanceof IOException) {
throw (IOException) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) …Run Code Online (Sandbox Code Playgroud) 假设我们尝试向java 8流应用一个可能抛出已检查异常的lambda:
Stream<String> stream = Stream.of("1", "2", "3");
Writer writer = new FileWriter("example.txt");
stream.forEach(s -> writer.append(s)); // Unhandled exception: java.io.IOException
Run Code Online (Sandbox Code Playgroud)
这不会编译.
一种解决方法是嵌套已检查的异常,RuntimeException但它使以后的异常处理变得复杂,而且它只是丑陋:
stream.forEach(s -> {
try {
writer.append(s);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
Run Code Online (Sandbox Code Playgroud)
另一种解决方法可能是转换有限的功能forEach,以普通的旧的foreach 循环是比较友好的检查的异常.
但天真的方法失败了:
for (String s : stream) { // for-each not applicable to expression type 'java.util.stream.Stream<java.lang.String>'
writer.append(s);
}
for (String s : stream.iterator()) { // foreach not applicable to type 'java.util.Iterator<java.lang.String>'
writer.append(s);
} …Run Code Online (Sandbox Code Playgroud) 我希望在不停止应用程序的情况下分析Java应用程序.我可以在应用程序运行时以某种方式添加Javaagent吗?
从我在示例中看到的春天pom.xml文件是他们为slf4j和log4j添加了一些条目,并且当你在spring应用程序中使用log4j时,它将被slf4j库包装.
有人可以向我解释这是如何神奇地发生的吗?
我有一个名为render_something的方法,可以创建大量的空格,例如:
<a href="#">#render_something('xxx')</a>
Run Code Online (Sandbox Code Playgroud)
结果可能是:
<a href="#">
something that generate from redner_something
</a>
Run Code Online (Sandbox Code Playgroud)
实际上我希望它是这样的:
<a href="#">something that generate from redner_something</a>
Run Code Online (Sandbox Code Playgroud)
速度有这样的东西吗?
#trim(#render_something('xxx'))
Run Code Online (Sandbox Code Playgroud) 当我使用openssl API验证服务器证书(自签名)时,我收到以下错误:
错误19在1深度查找:证书链中的自签名证书
根据openssl 文档,这个错误(19)是
"X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:证书链中的自签名证书 - 可以使用不受信任的证书构建证书链,但无法在本地找到根证书."
为什么会出现此错误?我的服务器证书有问题吗?
DBMS_JOB和DBMS_SCHEDULER有什么区别?
我试图找到一个数据结构,从一系列值中获取特定值并将其映射到一个键.
例如,我有以下条件:
我的值为5,我想将其映射到一个键.所以基于上述条件,我应该将它映射到B.
Java中是否有任何人可以向我推荐解决问题的数据结构?
目前我使用的哈希表只能将值映射到键.我尝试将值范围映射到哈希表中存在的特定值.但是,我陷入了将值范围映射到特定值的问题.所以现在我试图用另一种方法将值的范围映射到键.有谁知道如何解决这个问题?
编辑:
感谢Martin Ellis,我决定使用TreeMap来解决这个问题.
我正在使用Ant构建脚本来整理基于Eclipse的应用程序以进行分发.
构建的一个步骤是检查构建文件夹中是否存在正确的库.我目前使用Ant命令.不幸的是,每次切换到新的Eclipse构建时我都必须修改脚本(因为版本号会更新).
我不需要检查版本号,我只需要检查文件是否存在.
那么,我该如何检查:
org.eclipse.rcp_3.5.0.*
Run Code Online (Sandbox Code Playgroud)
代替:
org.eclipse.rcp_3.5.0.v20090519-9SA0FwxFv6x089WEf-TWh11
Run Code Online (Sandbox Code Playgroud)
用Ant?
欢呼,伊恩
我正在玩Java 8可完成的期货.我有以下代码:
CountDownLatch waitLatch = new CountDownLatch(1);
CompletableFuture<?> future = CompletableFuture.runAsync(() -> {
try {
System.out.println("Wait");
waitLatch.await(); //cancel should interrupt
System.out.println("Done");
} catch (InterruptedException e) {
System.out.println("Interrupted");
throw new RuntimeException(e);
}
});
sleep(10); //give it some time to start (ugly, but works)
future.cancel(true);
System.out.println("Cancel called");
assertTrue(future.isCancelled());
assertTrue(future.isDone());
sleep(100); //give it some time to finish
Run Code Online (Sandbox Code Playgroud)
使用runAsync我计划执行等待锁存器的代码.接下来我取消了未来,期望被抛入中断的异常.但似乎线程在await调用上仍然被阻塞,即使未来被取消(断言传递),也不会抛出InterruptedException.使用ExecutorService的等效代码按预期工作.它是CompletableFuture中的错误还是我的示例中的错误?