0

I have a java application that uses aws kinesis stream.

In the application, I get the student data in real time via kinesis stream and I have to make some action depending on the student id.

There is a list of students that I need to query from the database, and the each student from kinesis stream needs a checking if the student is in the list or not.

However I would like to query the list of students to the database only once, because that is one of the requirements.

On top of that I would like to make the query as soon as the application starts; that way if the query fails we can also fail the application. In other words, if the query does not succeed the application should not run.

This is what I would like to do

@SpringBootApplication
@EnabledScheduling
@EnableCaching
@Configuration
class Application implements CommandLineRuner {

  @Autowired
  RestTemplate restTemplate

  @Autowired
  List<Student> students

  static void main(String[] args) {
    SpringApplication app = new SpringApplication(Application);
    app.webApplicationType = WebApplicationType.NONE;
    app.run(args)
  }

  @Overrde
  void run(String... args) thows Exception {
    try {
      // make a request
      List<Student> people = this.restTemplate.exchange("https://some.url.com", ...other params)
      this.students = people

      // running kcl worker
      KclConsumer.kclWorker.run()
    } catch (Exception ex) {
      ....
    }
  }
}
@Service
class SomeService {

  @Autowired
  RestTemplate restTemplate

  @Autowired
  List<Student> students

  void func(Student id) {
    this.students.each {
      if(id == it.id) {
        println("found you")
        return;
      }
    }
    println("not found")
  }
}

I have tried creating the bean and share across the project as following but it does not seem to work...

class SomeConfig {
  @Bean
  List<Student> students() {
    return new ArrayList<>();
  }
}

I am guess how I am using bean is wrong. Can someone help me with my code please? Thank you in advance

2 Answers2

0

In theory, providing it in a bean should work, but you should be sure to initialize its data in the bean.

@Bean
public List<Student> getStudents() {
    RestTemplate restTemplate = new RestTemplate();
    List<Student> people = restTemplate.exchange("https://some.url.com", ...other params);
    return people;
}

This bean should be a singleton by default, so it should be the same list provided anywhere else a List<Student> is injected (either through constructor injection or with @Autowired)

If you are needing to use the RestTemplate in other places, you can make that a bean as well, which I believe you’ve already done, and inject it via constructor.

@Bean
public List<Student> getStudents(RestTemplate restTemplate) {
    List<Student> people = restTemplate.exchange("https://some.url.com", ...other params);
    return people;
}

Edit: I did a bit more research and it seems to make the list accessible, you may need to use @Resource instead of @Autowired as suggested here how to define a list bean in spring

Calvin P.
  • 1,116
  • 2
  • 8
  • 13
0

Don't make a bean of List<Student>. Make a business class with a public List<Student> getStudents() method - this class should be a bean.

ILya Cyclone
  • 806
  • 6
  • 16