Spring Boot Application as a Service

Spring Boot application as a Service

The following works for springboot 1.3 and above:

As init.d service

The executable jar has the usual start, stop, restart, and status commands. It will also set up a PID file in the usual /var/run directory and logging in the usual /var/log directory by default.

You just need to symlink your jar into /etc/init.d like so

sudo link -s /var/myapp/myapp.jar /etc/init.d/myapp

OR

sudo ln -s ~/myproject/build/libs/myapp-1.0.jar /etc/init.d/myapp_servicename

After that you can do the usual

/etc/init.d/myapp start

Then setup a link in whichever runlevel you want the app to start/stop in on boot if so desired.


As a systemd service

To run a Spring Boot application installed in var/myapp you can add the following script in /etc/systemd/system/myapp.service:

[Unit]
Description=myapp
After=syslog.target

[Service]
ExecStart=/var/myapp/myapp.jar

[Install]
WantedBy=multi-user.target

NB: in case you are using this method, do not forget to make the jar file itself executable (with chmod +x) otherwise it will fail with error "Permission denied".

Reference

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/html/deployment-install.html#deployment-service

How to run Spring Boot Application as a Service?

you can use this simple command to run a jar file as background service...

 javaw -jar test.jar 

after run this command you could not detect any change in cmd...and can close your command prompt. after 1 or 2 minute enter your URL in browser .. you will see your web program is running...

before run this command must be stop previously running jar for avoid same port conflict

for more details

How to call a service from Main application calls Spring Boot?

you can create a class that implements CommandLineRunner and this will be invoked after the app will start

@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {
@Autowired
private MyService myService;

@Override
public void run(String...args) throws Exception {
myService.save();

}
}

you can get farther information on this here

What is the use of service layer in Spring Boot applications?

Service layer is not a concept exclusive from Spring Boot. It's a software architectural term and frequently referred as a pattern. Simple applications may skip the service layer. In practical terms, nothing stops you from invoking a repository method from the controller layer.

But, I strongly advise the usage of a service layer, as it is primarily meant to define the application boundaries. The service layer responsibilities include (but are not limited to):

  • Encapsulating the business logic implementation;
  • Centralizing data access;
  • Defining where the transactions begin/end.

Quoting the Service Layer pattern from Martin Fowler's Catalog of Patterns of Enterprise Application Architecture:

A Service Layer defines an application's boundary and its set of available operations from the perspective of interfacing client layers. It encapsulates the application's business logic, controlling transactions and coor-dinating responses in the implementation of its operations.

Is it possible to get a list of service that dynamically get down on my application that is on spring boot

Spring Boot actuator provides a lot of useful endpoints, one of which is the health endpoint. The health endpoint returns the health status of your application based on its dependencies (databases, third party APIs, ...).

There are already builtin health indicators for RabbitMQ, Hazelcast and Elastic. There is no builtin health indicator for DynamoDB as far as I know, but you can also write your own health indicator, as seen in this question.

Now, to send you an email there are a two options:

  1. You can use an external program (eg. monitoring software) to regularly check the health actuator
  2. You can write it as part of your Spring boot application

Using an external program

If you use an external program, you can make it consume the /actuator/health endpoint. To enable this endpoint, configure the following property:

management.endpoints.web.exposure.include=health

By default this endpoint only exposes a single aggregated status of all health indicators combined. If you want to individually see which service is down, you can show more details by setting the following property to always or when_authorized:

management.endpoint.health.show-details=always | when_authorized

The response will look something like this:

{
"status": "DOWN",
"rabbit": {
"status": "DOWN",
"error": "org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)"
}
}

Writing your own

You can write a scheduled task by using the @Scheduled annotation to regularly check the health status. To obtain the health status, you can autowire HealthEndpoint as seen in this question.

Then you can send an email with Spring.

@Component
public class HealthEmailSender {
private final HealthEndpoint healthEndpoint;
private final JavaMailSender mailSender;

// TODO: Implement constructor to autowire healthEndpoint + mailSender

@Scheduled(fixedRate = 10 * 60 * 1000) // Check once every 10 minutes
public void sendEmailWhenBadHealth() {
if (isHealthDown()) {
sendMail();
}
}

private boolean isHealthDown() {
return Status.DOWN.equals(healthEndpoint.health().getStatus());
}

private void sendMail() {
MimeMessage message = mailsender.createMimeMessage();
// TODO: Set from / to / title / content
mailSender.send(message);
}
}

This code snippet above would send an e-mail as soon as any health indicator goes down (not only from the services you mentioned).

To obtain the health status of one of the services you're interested in, you can use healthEndpoint.healthForPath("rabbit").getStatus().



Related Topics



Leave a reply



Submit