小编s1m*_*r3d的帖子

Spring Batch + Spring Boot-关闭过程

我是Spring Batch的新手,我不知道如何在完成工作后终止Spring Boot进程。

在我的BatchConfiguration类中,我配置作业和步骤:

@Configuration
@EnableBatchProcessing
@EnableAutoConfiguration
public class BatchConfiguration {

   @Autowired
   private JobBuilderFactory jobBuilderFactory;

   @Autowired
   private StepBuilderFactory stepBuilderFactory;

   @Autowired
   private MyExecutionListener listener;

   @Autowired
   private MyTasklet step1Task;

   @Bean
   public Job initJob() throws Exception {
        JobBuilder jobBuilder = jobBuilderFactory.get("my-job").incrementer(new RunIdIncrementer())
                .listener(listener);

        FlowBuilder<FlowJobBuilder> builder = jobBuilder.flow(step1()).next(step2()).next(step3());

        return builder.build().build();
    }

    @Bean
    public Step step1() {
       return stepBuilderFactory.get("step1").tasklet(step1Task).build();
    }

    // and so on
Run Code Online (Sandbox Code Playgroud)

运行spring boot应用程序之后,并且每个步骤都完成了,我仍然在运行该进程。作业执行完成后如何停止?

java spring-batch spring-boot

6
推荐指数
0
解决办法
1959
查看次数

JPA加入特定领域

我有这样的场景:

User及其相关的UserRole实体类,如下:

@Entity
@Table(name="USER")
public class User implements Serializable {

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name="ID", unique=true, nullable=false)
   private int id;

   @Column(name="USERNAME", unique=true, nullable=false, length=255)
   private String username;

   @OneToMany(mappedBy="user")
   private List<UserRole> userRoles;
}
Run Code Online (Sandbox Code Playgroud)

@Entity
@Table(name="user_roles")
public class UserRole implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="user_role_id", unique=true, nullable=false)
    private int userRoleId;

    @Column(nullable=false, length=45)
    private String role;

    @ManyToOne
    @JoinColumn(name="username", nullable=false)
    private User user;
}
Run Code Online (Sandbox Code Playgroud)

现在,我需要查询具有特定角色的所有用户。我正在尝试使用 JPA 规范进行连接,如下所示:

Join<User, UserRole> join = root.join(User_.userRoles);
Expression<String> match = join.get(UserRole_.role);                    
Predicate predicate = builder.equal(match, "ROLE_USER");
Run Code Online (Sandbox Code Playgroud)

问题是生成的联接将在 …

java hibernate specifications jpa join

5
推荐指数
1
解决办法
2万
查看次数

Spring data Mongo Audit 字段反映在嵌套文档中

当保存带有嵌套审核文档的审核(@CreatedDate、@LastModifiedDate)文档时,这两个日期也将反映在嵌套文档中。

这是场景:

文件A.java

public class DocumentA {
   @Id
   private String id;
   @Version
   private Long version;
   @CreatedDate
   private Long createdDate;
   @LastModifiedDate
   private Long lastModifiedDate;

   // getters and setters
}
Run Code Online (Sandbox Code Playgroud)

文件B.java

public class DocumentB {
   @Id
   private String id;
   @Version
   private Long version;
   @CreatedDate
   private Long createdDate;
   @LastModifiedDate
   private Long lastModifiedDate;
   
   private DocumentA docA;

   // getters and setters
}
Run Code Online (Sandbox Code Playgroud)

DocumentA 已存储在数据库中,并设置了其createdDate 和lastModifiedDate。然后,当保存带有嵌套 DocumentA 的新 DocumentB 时,嵌套 DocumentA 的 2 个日期将被修改为刚刚为 DocumentB 设置的相同值。这种情况仅发生在嵌套文档中,而存储的 DocumentA 不会被触及(幸运的是!)。预期的行为是嵌套文档将保持与通过代码设置的完全相同(这意味着与原始文档A相同)

java audit spring-data spring-data-mongodb spring-repositories

5
推荐指数
1
解决办法
808
查看次数

Spring Data MongoDB存储库 - JPA规范之类的

是否有像Spring Data MongoDB存储库的JPA规范

如果没有,我如何使用存储库进行动态查询

经典场景可以是具有用户将填充的可选字段的搜索表单.

spring dynamic-queries dynamicquery spring-data spring-data-mongodb

3
推荐指数
1
解决办法
3349
查看次数

Java Future - Spring Authentication在AuditorAware中为null

这是我的情景:

我的应用程序启用了Mongo审核,使用自定义AuditorAware从当前用户获取SecurityContext.这适用于同步方法,并且当前审计员已成功保存,但我无法使其与@Async方法一起正常工作.

我有一个异步方法(CompletableFuture),可以对我的Mongo数据库进行一些更新.当AuditorAware.getCurrentAuditor()被调用时,没有任何身份验证信息存在,我不能让现任核数师(SecurityContextHolder.getContext().getAuthentication()回报null).

@Override
public User getCurrentAuditor() {
   Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

   if (authentication == null || !authentication.isAuthenticated()
                || authentication instanceof AnonymousAuthenticationToken) {
            log.error("Not authenticated");
            return null;
    }

    [...]

}
Run Code Online (Sandbox Code Playgroud)

我用的是DelegatingSecurityContextAsyncTaskExecutor:

@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(20);
        executor.setMaxPoolSize(100);
        executor.setQueueCapacity(200);
        executor.initialize();

        return new DelegatingSecurityContextAsyncTaskExecutor(executor);
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new ItacaExceptionHandler();
    } …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security mongodb completable-future

3
推荐指数
2
解决办法
1926
查看次数

使用默认值对Spring @Scheduled进行参数化

我需要@Scheduled使用属性文件中的值(如果存在)或默认值(如果不存在)对方法进行参数化。

我们可以通过以下方式从配置文件属性中进行参数化:

@Scheduled(cron = "${my.task.cron-exec-expr}")
public void scheduledTask() {
    // do something
}
Run Code Online (Sandbox Code Playgroud)

但是如果该属性不存在,我们将有一个运行时异常。

我尝试使用@ConfigurationProperties具有默认值的bean,但没有成功:

@Component
@ConfigurationProperties(prefix = "my.task")
public class MyTaskProperties {

    private String cronExecExpr = "*/5 * * * * *";

    // getter and setter
}
Run Code Online (Sandbox Code Playgroud)

如何避免这种情况并传递默认值?

java scheduled-tasks spring-scheduled

2
推荐指数
1
解决办法
1618
查看次数