Pintu
Pintu

Reputation: 102

Standard way to run a Java program continously

What is standard(industry standard) way to keep a Java program running continuously.

Use case(Realtime processing):

Found few questions in Stackoverflow, for example: https://stackoverflow.com/a/29930409/2653389

But my question is specific to what is the industry standard to achieve this .

Upvotes: 0

Views: 1550

Answers (2)

J-Alex
J-Alex

Reputation: 7107

First of all, there is no specified standard.

Possible options:

  • Java EE WEB application
  • Spring WEB application
  • Application with Spring-kafka (@KafkaListener)

Kafka producer will potentially accept some commands. In real-life scenario I worked with applications which runs continuously with listeners, receiving requests they triggered some jobs, batches and etc.

It could be achieved using, for example:

  • Web-server accepting HTTP requests
  • Standalone Spring application with @KafkaListener

Consumer could be a spring application with @KafkaListener.

@KafkaListener(topics = "${some.topic}")
public void accept(Message message) {
   // process
}

Spring application with @KafkaListener will run infinitely by default. The listener containers created for @KafkaListener annotations are registered with an infrastructure bean of type KafkaListenerEndpointRegistry. This bean manages the containers' lifecycles; it will auto-start any containers that have autoStartup set to true. KafkaMessageListenerContainer uses TaskExecutor for performing main KafkaConsumer loop.

Documentation for more information.

If you decide to go without any frameworks and application servers, the possible solution is to create listener in separate thread:

public class ConsumerListener implements Runnable {

    private final Consumer<String, String> consumer = new KafkaConsumer<>(properties);

    @Override
    public void run() {
        try {
            consumer.subscribe(topics);

            while (true) {
                    // consume
                }
            }
        } finally {
            consumer.close();
        }
    }
}

Upvotes: 3

Danila Zharenkov
Danila Zharenkov

Reputation: 1863

When you start your program like "java jar" it will work until you didn't stop it. But that is OK for simple personal usage and testing of your code. Also in UNIX system exist the app called "screen", you can run you java jar as a daemon.

The industry standard is application servers. From simple Jetty to enterprise WebSphere or Wildfly(ex. JBoss). The application servers allows you to run application continiously, communicate with front-end if neccassary and so on.

Upvotes: 1

Related Questions