我正在使用Spring Boot开发Rest API,并且必须访问应用程序的端点。我曾经用过RestTemplate
。我能够使用2种方法来做到这一点,
postForEntity()
:
responseEntity =
restTemplate.postForEntity(uri, httpEntity, ResponseClass.class);
Run Code Online (Sandbox Code Playgroud)exchange()
:
responseEntity =
restTemplate.exchange(uri, HttpMethod.POST, httpEntity, ResponseClass.class);
Run Code Online (Sandbox Code Playgroud)我想知道这两种方法的用法和区别。
我也看到了另一种方法execute()
。请阐明一下。如何以及何时使用它。
我是 SMTP 相关概念的新手。我目前正在使用 Nodemailer 开发一个 nodejs 电子邮件客户端。其中,我无法收到退回邮件 (NDR)。我在下面的查询,
为此,我应该配置 DSN 还是信封?我想我对 dsn 和信封选项感到困惑。
nodemailer 如何获取退回的邮件?这需要任何单独的配置吗?或者任何事件监听器?
提前致谢。
我有一个代码,我可以在循环中执行一段逻辑,并使用 Flux 之间有一些延迟。像这样的事情,
Flux.defer(() -> service.doSomething())
.repeatWhen(v -> Flux.interval(Duration.ofSeconds(10)))
.map(data -> mapper(data)) //map data
.takeUntil(v -> shouldContinue(v)) //checks if the loop can be terminated
.onErrorStop();
Run Code Online (Sandbox Code Playgroud)
现在,我想要增量延迟。这意味着,在前 5 分钟内,每次执行之间的延迟可能为 10 秒。然后,在接下来的 10 分钟内,延迟可以是 30 秒。并且,此后每次执行之间的延迟可以是一分钟。
我如何使用 Flux 来实现这一目标?
提前致谢。
嗨,我是Spring批处理的新手,并且遇到了以下无法解决的异常:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:781) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at com.demo.BatchDemo.main(KnpBatchApplication.java:16) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_72]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_72]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_72]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_72]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.1.RELEASE.jar:2.0.1.RELEASE]
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653) ~[na:1.8.0_72]
at java.util.ArrayList.get(ArrayList.java:429) ~[na:1.8.0_72]
at org.springframework.batch.core.JobParametersBuilder.getNextJobParameters(JobParametersBuilder.java:265) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:162) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:179) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:134) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:128) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
... 10 common frames omitted
Run Code Online (Sandbox Code Playgroud)
我的代码在这里:
@SpringBootApplication
public class …
Run Code Online (Sandbox Code Playgroud) 我试图了解 node.js 单线程架构和事件循环,以使我们的应用程序更高效。因此,请考虑这种情况,我必须为 http api 调用进行多次数据库调用。我可以使用Promise.all()
或使用单独的await
.
例子:
使用异步/等待
await inserToTable1();
await insertToTable2();
await updateTable3();
Run Code Online (Sandbox Code Playgroud)
使用Promise.all()
我可以做同样的事情
await Promise.all[inserToTable1(), insertToTable2(), updateTable3()]
Run Code Online (Sandbox Code Playgroud)
这里对于给定时间的一个 API 命中,Promise.all()
将更快地返回响应,因为它并行触发 DB 调用。但是,如果我每秒有 1000 个 API 命中,会有什么不同吗?对于这种情况,事件Promise.all()
循环更好吗?
更新 假设如下,通过 1000 个 API 命中,我指的是应用程序的总体流量。考虑有 20-25 个 API。其中一些可能会执行数据库操作,一些可能会进行一些 http 调用等。此外,我们永远不会达到数据库池的最大连接数。
提前致谢!!
嗨,我最近一直在 Spring Batch 工作,需要一些帮助。
1)我想使用多个线程运行我的作业,因此我使用了 TaskExecutor,如下所示,
@Bean
public TaskExecutor taskExecutor() {
SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
taskExecutor.setConcurrencyLimit(4);
return taskExecutor;
}
@Bean
public Step myStep() {
return stepBuilderFactory.get("myStep")
.<MyEntity,AnotherEntity> chunk(1)
.reader(reader())
.processor(processor())
.writer(writer())
.taskExecutor(taskExecutor())
.throttleLimit(4)
.build();
}
Run Code Online (Sandbox Code Playgroud)
但是,在执行时可以在控制台中看到下面的行。
osbclsupport.SimpleJobLauncher : 没有设置 TaskExecutor,默认为同步执行器。
这是什么意思?但是,在调试时,我可以看到四个 SimpleAsyncExecutor 线程正在运行。有人可以对此有所了解吗?
2)我不想使用 spring 批处理创建的元数据表运行我的批处理应用程序。我试过添加spring.batch.initialize-schema=never
. 但它没有用。我还看到了一些通过使用ResourcelessTransactionManager
,来做到这一点的方法MapJobRepositoryFactoryBean
。但是我必须为我的工作进行一些数据库事务。那我用这个可以吗?我也能够通过扩展DefaultBatchConfigurer
和覆盖来做到这一点:
@Override
public void setDataSource(DataSource dataSource) {
// override to do not set datasource even if a datasource exist.
// initialize will use a Map …
Run Code Online (Sandbox Code Playgroud) spring-boot ×3
java ×2
node.js ×2
spring ×2
spring-batch ×2
async-await ×1
delay ×1
email ×1
event-loop ×1
flux ×1
javascript ×1
nodemailer ×1
promise ×1
resttemplate ×1
smtp ×1