Java 9附带了用于创建不可变列表的便捷工厂方法.最后,列表创建非常简单:
List<String> list = List.of("foo", "bar");
Run Code Online (Sandbox Code Playgroud)
但是这个方法有12个重载版本,11个有0到10个元素,另一个有var args.
static <E> List<E> of(E... elements)
Run Code Online (Sandbox Code Playgroud)
同样是与案件Set和Map.
由于存在var args方法,有多少11种方法有什么意义呢?
我认为var-args创建一个数组,所以其他11个方法可以跳过创建一个额外的对象,在大多数情况下会有0-10个元素.还有其他原因吗?
我最近不得不升级到aws-java-sdk 1.11.108.我有一个java程序,它将s3对象(大小为8到10 GB)下载到EC2框并将其作为流处理.这个程序已经工作了2年多没有任何问题,但在更新到aws-java-sdk的最新版本后,我的文件下载在日志中中止了以下WARN消息(无例外)
WARN:com.amazonaws.services.s3.internal.S3AbortableInputStream - Not all bytes were read from the S3ObjectInputStream, aborting HTTP connection. This is likely an error and may result in sub-optimal behavior. Request only the bytes you need via a ranged GET or drain the input stream after use.
S3Object s3Obj = s3client.getObject(new GetObjectRequest(bucketName, s3FileName));
Reader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(s3Obj.getObjectContent());
Run Code Online (Sandbox Code Playgroud)
如果有人可以告诉为什么流静默地中止而不抛出任何异常,我会很感激,以及使它工作的最佳方法是什么.
谢谢
我写了下面的代码,并惊讶地看到输出:
Integer a = 211;
int b = 211;
int[] array = {210,211,212};
System.out.println(Arrays.asList(array).contains(a));
System.out.println(Arrays.asList(array).contains(b));
Run Code Online (Sandbox Code Playgroud)
输出:
false
false
Run Code Online (Sandbox Code Playgroud)
我发现了这个问题以及与之相关的一些其他问题,并了解到该asList方法不是Autobox的东西.我在eclipse javadoc预览中检查了返回的类型:

我无法理解这种返回类型.int[]是一个对象,而不是一个原始,所以它很好.我确定我没有得到List<Integer>(我预期的东西),但我不确定如何使用返回的东西.我的问题是:
我正在使用以下代码根据此处X509Certificate的参考资料验证 a 。
static void verifyCertTrust(X509Certificate certificate, Set<X509Certificate> additionalCerts) throws CertificateException, NoSuchAlgorithmException, NoSuchProviderException, CertPathValidatorException, InvalidAlgorithmParameterException, CertPathBuilderException{
Set<X509Certificate> trustedRoots = new HashSet<X509Certificate>();
Set<X509Certificate> intermediateCerts = new HashSet<X509Certificate>();
for (X509Certificate cert : additionalCerts) {
if(isSelfSigned(cert)){
trustedRoots.add(cert);
}
else{
intermediateCerts.add(cert);
}
}
Set<TrustAnchor> trustAnchors = new HashSet<TrustAnchor>();
for (X509Certificate root : trustedRoots) {
trustAnchors.add(new TrustAnchor(root, null));
}
X509CertSelector selector = new X509CertSelector();
selector.setCertificate(certificate);
PKIXParameters parameters = new PKIXBuilderParameters(trustAnchors, selector);
parameters.setRevocationEnabled(false);
CertStore intermediateCertStore = CertStore.getInstance("Collection", new CollectionCertStoreParameters(intermediateCerts), "BC");
parameters.addCertStore(intermediateCertStore);
CertPathBuilder cpb …Run Code Online (Sandbox Code Playgroud) 我有需要执行的发言权两个任务task1和task2它们是相同的业务流程的一部分.我必须在task1完成时给最终用户做出响应,因此必须最小化响应时间.
我目前的方法是执行task1,一旦task1完成,就task2异步调用方法.task2很复杂,它的响应时间不受我的控制,因为它有一些外部依赖.
@Stateless
public class SessionBean1 {
@Inject
SessionBean2 sessionBean2;
public void doTask1(){
// task one stuff
sessionBean2.doTask2();
}
}
@Stateless
public class SessionBean2 {
@Asynchronous
public void doTask2(){
// do task2 stuff
}
}
Run Code Online (Sandbox Code Playgroud)
在websphere 8.0(使用中的EJB容器)中,同步方法和异步方法由不同的线程池运行.
我最初的假设是即使task2表现不好,task1也没有影响,但遗憾的是,这不是真的.
如果task2执行得很糟糕,异步线程池中的所有线程都将被占用.这将导致task1等待异步线程空闲,从而task1产生影响.
websphrere服务器日志中的消息:
The request buffer for thread pool WorkManager.WebSphere_EJB_Container_AsynchMethods_Internal_WorkManager has reached its capacity
我的问题是什么是实现我在这里想要实现的目标的正确方法.
背景:我在tomcat上部署了一个使用基于表单的身份验证的javaee webapp.当Web服务器收到登录请求时,它会将请求发送到专用的身份验证服务,该服务验证用户登录(用户ID和密码).成功验证后,用户的会话将在Web服务器中维护.
问题:我在这里编写了一个简单的webpp 源代码,以模拟场景.成功登录后,当前HttpSession实例将失效并创建新实例.对于后登录页面的每个请求,会话都经过验证.设置了一个新JSESSIONIDcookie,用于在会话期间识别用户,直到会话过期或用户注销.可以在浏览器的开发工具中轻松查看此cookie.如果我复制cookie并通过JavaScript(document.cookie="JSESSIONID=xyzz")在不同的浏览器中设置它,然后尝试访问帖子登录页面,则服务器将其识别为有效请求并成功验证会话.提供帖子登录页面,用户不会因用户ID和密码而受到质疑.
POC:用户打开浏览器和输入网址http://localhost:8080/mywebapp/与和日志admin和pass1234.成功登录后http://localhost:8080/mywebapp/home会显示主页.现在,JSESSIONIDCookie被复制并在FireFox中设置.用户进入http://localhost:8080/mywebapp/homeFirefox并显示主页而不会受到userId和密码的质询.
问题:如何通过多个浏览器复制相同的会话,如何防止这种情况?
我正在使用一个使用动态bean注册的配置类:
@Configuration
public class ConfigClass {
@Autowired
private GenericApplicationContext applicationContext;
@PostConstruct
private void init() {
System.out.println("init");
applicationContext.registerBean("exService", ExecutorService.class, () -> Executors.newFixedThreadPool(10), bd -> bd.setAutowireCandidate(true));
System.out.println("init done");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试自动装配bean,应用程序启动会失败并显示错误 Field exService in com.example.DemoApplication required a bean of type 'java.util.concurrent.ExecutorService' that could not be found.
从日志中我可以看到,在错误之前没有调用config类的init方法,因为没有打印出两个系统输出语句.
但是,当我使用applicationContext.getBean(ExecutorService.class)它确实没有任何问题.
无论如何,我可以把豆子送到Autowire?
我故意不使用@Bean注释,因为我需要根据某些条件动态注册bean.
经过几个小时的搜索,我终于意识到java.sql.ResultSet不是Serializable,也没有办法做到这一点.我尝试添加到List,作为Serializable对象中的实例变量和其他东西,但事情结果是天真和绝望的尝试.我尝试使用RowSet的实现,如CachedRowSetImpl,它们是Serializable但是它们会增加响应时间,很可能是因为它们迭代了ResultSet.最重要的是,除非您选择迭代ResultSet,否则您无法通过网络发送它包含的数据.
我知道我必须迭代的替代方案并将内容添加到数据模型对象和列表中,但我绝望地想知道这背后的理性是什么?那时java的开发人员想到了什么?
Nginx 负责从根目录到根 URL 的所有静态内容。例如,如果根内容位置配置为/usr/share/nginx/html包含文件/usr/share/nginx/html/foo.html,则 urlhttp://localhost/foo.html将提供该 html 文件。我想在 URL 中添加上下文路径前缀,以便为文件http://localhost/myapp/foo.html提供服务。/usr/share/nginx/html/foo.html
我尝试更改位置并添加别名,但这给出了 404。
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /myapp/ {
alias /usr/share/nginx/html/;
}
Run Code Online (Sandbox Code Playgroud)
我也希望那http://localhost/myapp应该服务/usr/share/nginx/html/index.html
我正在使用 nginx 的1.12.1-alpinedocker 镜像
这是我的代码:
Scanner in = new Scanner(System.in);
int option;
do{
System.out.println("1. Add Account");
System.out.println("2. Check Balance");
System.out.println("5. Exit");
System.out.print("Enter Choice >> ");
option = in.nextInt();
Account account = null;
switch (option) {
case 1:
try{
System.out.print("Enter id >> ");
int id = in.nextInt();
System.out.print("Enter amount >> ");
double bal = in.nextDouble();
account = new Account(id, bal);
}
catch (InputMismatchException e) {
System.out.println("Invalid input, try again");
}
break;
case 2:
System.out.println(account.getBalance()); // null pointer access here
break;
default:
System.out.println("Invalid option");
break;
} …Run Code Online (Sandbox Code Playgroud) java ×7
java-ee ×2
arrays ×1
asynchronous ×1
autoboxing ×1
autowired ×1
aws-java-sdk ×1
bouncycastle ×1
collections ×1
cookies ×1
ejb ×1
java-9 ×1
jce ×1
jdbc ×1
list ×1
nginx ×1
servlets ×1
session ×1
spring ×1
spring-boot ×1
sql ×1
tomcat ×1
websphere ×1