所以我有一个 Java (Spring Boot) 应用程序,我在其中使用了 Amazon 的 RSA 密钥。目前这是.pem格式并存储在我的项目中的本地文件夹中。
但是,当我将应用程序部署到 Web (AWS) 时,我应该在哪里存储它:
可以以其他格式存储它并最好在 application.properties 中使用它吗?
我可以使用 RSA 密钥作为字符串还是它总是来自文件?
*或者我是否需要将其存储在服务器中的安全位置,它会在哪里以及它的安全性如何?
我正在阅读ScheduledThreadPoolExecutor JavaDoc并遇到以下问题:
延迟任务执行不早于已启用,但没有任何 时候,他们都启用后约实时保证,他们将 开始。按照提交的先进先出(FIFO)顺序启用计划执行时间完全相同的任务。
所以,如果我写这样的话:
ScheduledExecutorService ses = Executors.newScheduledThreadPool(4); //uses ScheduledThreadPoolExecutor internally
Callable<Integer> c;
//initialize c
ses.schedule(c, 10, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
是否可以保证在计划后的10秒内开始执行可调用对象?据我所知,该规范允许它甚至在计划后的几个小时内执行(没有任何实时保证,如文档中所述)。
在实践中如何运作?我应该延迟很长时间吗?
我有一个使用Spring依赖注入的Java应用程序。我想模拟一个bean,并验证它是否接收到某些方法调用。
问题是Mockito不会在测试之间重置模拟,因此我无法正确验证其上的方法调用。
我的测试单元:
public class MyClass {
@Resource
SomeClientClass client;
public void myMethod() {
client.someMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
单元测试课:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = UnitTestConfig.class)
public class MyClassTest {
@Resource
SomeClientClass client;
@Test
public void verifySomething() {
// ...
Mockito.verify(client).publish();
}
}
Run Code Online (Sandbox Code Playgroud)
最后,
@Configuration
public class UnitTestConfig {
@Bean
SomeClientClass client() {
return Mockito.mock(SomeClientClass.class);
}
}
Run Code Online (Sandbox Code Playgroud)
尽管我可以通过在测试之间手动重置模拟来解决这个问题,但是我想知道是否存在更干净/更惯用的方法。
我想在特定时间触发 Java 代码,比如说每天凌晨 1 点。
我写了一个线程如下:
while (true) {
if (currentTime = 1AM) {
doSomething;
}
}
Run Code Online (Sandbox Code Playgroud)
我有点担心,while loop继续运行,会不会减慢机器速度,或者吃掉处理资源?
我的第一个问题是,我在想如果我每秒只循环 1 次,会更好吗?
while (true) {
if (currentTime = 1AM) {
doSomething;
}
Thread.sleep(1000);
}
Run Code Online (Sandbox Code Playgroud)
我的第二个问题是,有时我看到while loop如下写,大部分时间是获取 Java 锁,如果我们在while loop下面这样写,谁能解释一下有多昂贵(对不起,如果这是非常基本的问题)?
while (isLock) {
// do nothing
}
doThisIfNoLock();
Run Code Online (Sandbox Code Playgroud)
我扩展上面的想法,如果我创建一个空线程,并且while loop内部无限空,那么线程实际消耗了多少资源(处理能力)?因为循环里面没有内容,按照我的想象,循环会跑的非常快,最终会占用很多个CPU周期?真的吗?
我正在审查github上的HikariCP项目,它声明它支持"Java 7和Java 8 maven artifact",在其源代码中,它使用了一些Java 8特性:
java.util.function.Consumer;
java.util.function.Predicate;
java.util.function.UnaryOperator;
Run Code Online (Sandbox Code Playgroud)
我想如果其他人使用Java 7引用此项目,则会发生错误.那么,该项目如何使其同时支持Java 7和Java 8?
我尝试使用elasticsearch容器编写测试.我用https://www.testcontainers.org/库运行它.那是我的配置:
@ClassRule
public static GenericContainer elasticContainer =
new GenericContainer("docker.elastic.co/elasticsearch/elasticsearch:5.3.0")
.withExposedPorts(9300, 9200)
.withEnv("xpack.security.enabled", "false")
.withEnv("transport.host", "127.0.0.1")
.withEnv("http.host", "0.0.0.0");
Run Code Online (Sandbox Code Playgroud)
我有一个例外:
org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{9fuJUZYWS6O6IgLGOgJDaA}{localhost}{127.0.0.1:32792}]
Run Code Online (Sandbox Code Playgroud)
我重新配置了我的端口进行测试,并且可以使用9200端口(在testcontainers映射的端口上) - 我通过curl检查了它.但9300不是.
有谁知道如何解决运输主机问题?
最近,我了解到.bss段存储未初始化的数据。但是,当我尝试如下的小程序并size(1)在终端中使用命令时,即使我添加了一些全局变量,.bss 段也没有改变。我误解了什么吗?
jameschu@aspire-e5-573g:~$ cat test.c
#include <stdio.h>
int main(void)
{
printf("hello world\n");
return 0;
}
jameschu@aspire-e5-573g:~$ gcc -c test.c
jameschu@aspire-e5-573g:~$ size test.o
text data bss dec hex filename
89 0 0 89 59 test.o
jameschu@aspire-e5-573g:~$ cat test.c
#include <stdio.h>
int a1;
int a2;
int a3;
int main(void)
{
printf("hello world\n");
return 0;
}
jameschu@aspire-e5-573g:~$ gcc -c test.c
jameschu@aspire-e5-573g:~$ size test.o
text data bss dec hex filename
89 0 0 89 59 test.o
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
public class Context {
private final Class<?> clazz;
private final String resource;
private final com.thirdparty.Context context;
public Context(final String resource, final Class<?> clazz) {
this.clazz = clazz;
this.resource = resource;
this.context = com.thirdparty.Context.newInstance(this.clazz);
}
public String marshall(final Object element) {
return this.context.marshall(element);
}
public Object unmarshall(final String element) {
return this.context.unmarshall(element);
}
}
Context context = new Context("request.xsd", Request.class);
// Marshall
Request request = new Request();
String xml = context.marshall(request);
// Unmarshall
Request roundTrip = Request.cast(context.unmarshall(xml));
Run Code Online (Sandbox Code Playgroud)
我试图用泛类版本的Context类替换它:
public class …Run Code Online (Sandbox Code Playgroud) 当我们编码和使用优先级队列时,优先级究竟代表什么?它是抽象的还是具体的,例如根据不同建筑物的高度进行分类?使用优先队列有什么好处?
构造函数的目的是初始化字段的值,设置对象的初始状态.那么如果构造函数中没有初始化某些字段或所有字段会发生什么?
它是否在用户定义构造函数之前调用JVM提供的默认构造函数?
那么,在这个例子中,会输出什么?
class Name{
int x;
boolean y;
Name(){
// no initialize
}
public static void main(){
Name n = new Name();
System.out.println(n.x + ", " + n.y);
}
}
Run Code Online (Sandbox Code Playgroud) java ×6
c ×1
concurrency ×1
containers ×1
generics ×1
java-7 ×1
java-8 ×1
linux ×1
loops ×1
maven ×1
mockito ×1
rsa ×1
spring ×1
spring-boot ×1
while-loop ×1