How to create a Async http call in Spring Boot

Use case in this example:

1. Client makes call to server side, returned immediately, but already start a asynchrony process in server side.

2. When the process is still running, client need to call http call to get the processing status from time to time.

3.  This example shows a processing status shared by the whole container context, as the Bean and controller scope in Spring is Singleton. You can also try to make controller and bean into session, request level if needed.Or use the session/Model to handle shared data.

4.  @Async can not be used in the controller , I test it. Callable maybe it the way for controller method.

 

– Let us take a look code.  Create one service bean and make it expose the status by a getter.

@Service
public class TestService {

private static final Logger logger = LoggerFactory.getLogger(TestService.class);

private String status = "Not start";

@Async("processExecutor")
 public void process() {
 logger.info("Received request to process in ProcessServiceImpl.process()");
 status = "in Process";
 try {
 Thread.sleep(15 * 1000);
 logger.info("Processing complete");
 } catch (InterruptedException ie) {
 logger.error("Error in ProcessServiceImpl.process(): {}", ie.getMessage());
 }
 
 status = "Done";
 
 }

public String getStatus() {
 return status;
 }
}

 

Then config the application to make it support the @EnableAsync and define the tread pool for Async calls.

@EnableAsync
public class MyDemoApplication {
...........................................................// for Async test code
 @Bean(name = "processExecutor")
 public TaskExecutor workExecutor() {
 ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
 threadPoolTaskExecutor.setThreadNamePrefix("Async-");
 threadPoolTaskExecutor.setCorePoolSize(3);
 threadPoolTaskExecutor.setMaxPoolSize(3);
 threadPoolTaskExecutor.setQueueCapacity(600);
 threadPoolTaskExecutor.afterPropertiesSet();
 logger.info("ThreadPoolTaskExecutor set");
 return threadPoolTaskExecutor;
 }

 

Then at spring mvc controller create two method for http , start process and check process status:

 

@Autowired
 private TestService processService;
....
@GetMapping("/startAsync")
 public Result startAsync() {
 processService.process();
 statusData = processService.getStatus();
 Result rt = new Result(true, statusData);
 return rt;
 }

@GetMapping("/getAsyncStatus")
 public Result getAsyncStatus() {
 statusData = processService.getStatus();
 Result rt = new Result(true, statusData);
 return rt;
 }

Then server side is done!

Let me create a client code to test it how is works:

 testStartAsyncCall();
 testAsyncStatusCall();
 try {
 TimeUnit.SECONDS.sleep(10);
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 testAsyncStatusCall();
 try {
 TimeUnit.SECONDS.sleep(10);
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 testAsyncStatusCall();

 

You can see the process take 15 seconds to finish, so client side try to get the status every 10 seconds after call to start the process. so we can see how status is changed in the whole process.

My example is modified from one example post, Please refer to this post:

http://softwaredevelopercentral.blogspot.com/2017/07/asynchronous-processing-async-in-spring.html

 

 

 

Advertisements

Exception Processing @RestControllerAdvice in Spring

A very useful class in the Spring 4 plus is the  @RestControllerAdvice, it combines the @ControllerAdvice inside. By defining a @RestControllerAdvice, when a controller thorw one exception, you can put all the exception handlers code into one class and you then can choose to use different @ExceptionHandler method to deal with different exceptions , or you even can deal all in one method to use the instance check up to decide what response msg you should send out in response;

So you have chance to reply user a friend msg of your runtime exception of your Restful API, and even with customized http status code too with ResponseEntity class.

Here I put a fake sample code to show how it works. you can do more study and use the handler in your way. Result is a class you defined Object your want to reply to client side.


@RestControllerAdvice
public class GlobalExceptionHandler {

private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

@ExceptionHandler(value = { MyCustomizedException.class })
 @ResponseStatus(HttpStatus.BAD_REQUEST)
public Result myCustomizedExceptionHandler(MyCustomizedException ex) {
 // ..... what ever process
 return new Result(500, 0001, ex.getMessage());
 }


@SuppressWarnings("rawtypes")
 @ExceptionHandler(value = Exception.class)
 public ResponseEntity handle(Exception e) {
 e.printStackTrace();
 Result rt = new Result(e.getMessage());

if (e instanceof SoaException) {
 rt = ((SoaException) e).getBodyStatus();
 } else if (e instanceof MissingServletRequestParameterException) {
 rt = DataUtil.rt("0002");
 } else if (e instanceof HttpRequestMethodNotSupportedException) {
 rt = DataUtil.rt("0003");
 } else if (e instanceof HttpMediaTypeNotSupportedException) {
 rt = DataUtil.rt("0004");
 } else if (e instanceof HttpMessageNotReadableException) {
 rt = DataUtil.rt("0005");
 } else if (e instanceof BindException) {
 rt = DataUtil.rt("0006");
 } else if (e instanceof NumberFormatException) {
 rt = DataUtil.rt("0007");
 } else if (e instanceof DataIntegrityViolationException) {
 rt = DataUtil.rt("0008");
 } else {
 rt = DataUtil.rt(Constants.ERROR_CODE);
 }
 LOGGER.error(rt.getMessage() + e);
 return new ResponseEntity<>(rt, HttpStatus.OK);
 }

 

Spring Admin Server and UI for Spring Boot Application – 2

Now , let us add the security login for the spring admin server.

1. Add these into pom.xml

<dependency>
 <groupId>de.codecentric</groupId>
 <artifactId>spring-boot-admin-server-ui-login</artifactId>
 <version>1.5.7</version>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-security</artifactId>
 </dependency>

spring-boot-admin-server-ui-login will supply the login and logout page.

 

2. Create a web security config class like this:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
 protected void configure(HttpSecurity http) throws Exception {
 http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
 http.logout().logoutUrl("/logout");
 http.csrf().disable();
 http.authorizeRequests().antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**").permitAll();
 http.authorizeRequests().antMatchers("/**").authenticated();
 http.httpBasic();
 }
}

 

3. put this into the application.properties

management.security.enabled=true
security.user.name=admin
security.user.password=*******

Restart and now you need login to access the server UI.

Screenshot from 2018-04-27 15-37-57

 

But now, you will lost your client app in the UI. To let the client can access the server URL to exchange the data to server, we also need to change client configuration:

Just add this into client application.properties, the server can connect with client again:

management.security.enabled = false
spring.boot.admin.username=admin
spring.boot.admin.password=*******

management.security.enabled = false is to tell the server no need the security policy to access the client URL of actuator.   But client will use admin account to login to the server side to submit data…

 

Here I have done the demo for the single client and server admin UI login.  Next step you need to look at in the discovery  and eureka environment, how to link one server with more clients in cluster.

Please refer the doc of admin server for more details.

https://codecentric.github.io/spring-boot-admin/1.5.7/

 

Spring Admin Server and UI for Spring Boot Application – 1

The one of best parts of the Spring boot  is it have some great tools can add on to your application to have basic feature of a production system. Beside last time swagger for Spring boot, this time we add the Spring Admin Server to the application.

To monitoring your system health, you need to create a admin server ruuning as a standalone application, eg at port 8081 this time. So from this server , we can admin all the spring boot applications’ health.

We will first create a new app to run the admin server first.

1. Create a new Spring boot app with “web” enabled.

2. Add this to pom.xml

<dependency>
 <groupId>de.codecentric</groupId>
 <artifactId>spring-boot-admin-starter-server</artifactId>
 <version>1.5.7</version>
 </dependency>

3. Add this to the application.properties

spring.application.name=ServersMonitor
server.port=8081

 

4. Just add the @EnableAdminServer to the application main class:

@EnableAdminServer
@SpringBootApplication
public class MonitorApplication {

public static void main(String[] args) {
 SpringApplication.run(MonitorApplication.class, args);
 }
}

Ok , sever is ready, start it and surf to http://localhost:8081  you will see the Admin UI but with no application be monitored there.

 

Now let change one app as the client that will be monitored by admin.

1. Add the dependency in pom.xml, bold text part!

<!-- Spring admin UI client ===================================================== -->
 <!-- Spring admin UI client ===================================================== -->
 <dependency>
 <groupId>de.codecentric</groupId>
 <artifactId>spring-boot-admin-starter-client</artifactId>
 <version>1.5.7</version>
 </dependency>
 </dependencies>
 <build>
 <plugins>
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 <executions>
 <execution>
 <goals>
 <goal>build-info</goal>
 </goals>
 </execution>
 </executions>
 </plugin>
 </plugins>
 </build>
</project>

2. Add this to the application.properties, as our admin server running at 8081

# =======================================================================================
# For Spring Admin UI client side
# =======================================================================================
spring.boot.admin.url = http://localhost:8081 
management.security.enabled: false

3. All done, just start our app, it is running at 8082 port.  then go back to admin server UI, we will see it auto has one application be monitored!

Screenshot from 2018-04-27 14-37-35

 

And we can check all health info about our client applications from here:

Screenshot from 2018-04-27 14-37-43.png

Bingo. have a rest and we will proceed to the security login part!

 

Thanks for this link about some process and basics.

http://www.baeldung.com/spring-boot-admin

 

Swagger for Spring Boot How To

This is the note about how to make the swagger work together with Spring Boot. Swagger here is used as great tool to automatically create the Restful API html docs and testing API web UI.

1. Add swagger2 and UI to POM.XML

<dependency>
 <groupId>io.springfox</groupId>
 <artifactId>springfox-swagger2</artifactId>
 <version>2.6.1</version>
 <scope>compile</scope>
 </dependency>

<dependency>
 <groupId>io.springfox</groupId>
 <artifactId>springfox-swagger-ui</artifactId>
 <version>2.6.1</version>
 <scope>compile</scope>
 </dependency>

2. Create a class for spring boot configuration for swagger:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static springfox.documentation.builders.PathSelectors.regex;

@EnableSwagger2
@Configuration
public class SwaggerConfig {

@Bean
 public Docket productApi() {
 return new Docket(DocumentationType.SWAGGER_2).select()
 .apis(RequestHandlerSelectors.basePackage("com.wayneshare.springboot.swaggerexample"))
 .paths(regex("/rest.*")).build().apiInfo(metaInfo());
 }

private ApiInfo metaInfo() {

ApiInfo apiInfo = new ApiInfo("WayneShare RestfulAPI", "Enable knowledge sharing ...", "1.0",
 "Terms of Service", new Contact("WayneShare", "https://www.wayneshare.com", "zhou_xiaowei@yahoo.com"),
 "", "");

return apiInfo;
 }
}

3. Then you can start run the application and goes to this URL http://localhost:8080/swagger-ui.html

You will see the swagger API html docs and can test over there.

4.  Try to change this part of code in config to see changes on the UI, then you will know the meta data reflecting on UI.

ApiInfo apiInfo = new ApiInfo("WayneShare RestfulAPI", "Enable knowledge sharing ...", "1.0",
 "Terms of Service", new Contact("WayneShare", "https://www.wayneshare.com", "zhou_xiaowei@yahoo.com"),
 "", "");

5. If you want to change the grouping name and description for one controller, then use @Api for you controller:

@RestController
@RequestMapping("/rest/account")
@Api(tags = "API 02", description = "Account info management")
public class AccountController {

6. Check these annotations too in case you want to change some meta on UI  and even the http return code desc. @ApiModelProperty @ApiOperation @ApiResponses

@ApiOperation(value = "Returns Authentication Result")
 @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful Authentication") })

7. You can also run the testing for each API. Here is the final result I have done for a sample project:

Screenshot from 2018-04-27 10-41-10

Enjoy it and credit goes two these two links

https://github.com/TechPrimers/spring-boot-swagger-example

https://github.com/indrabasak/swagger-deepdive/wiki/Renaming-and-Sorting-REST-Controllers

RabbitMQ for Spring Boot with Jackson

Issue:

When you use rabbitMQ to send the java object you might get this exception. The error you will meet would like this :

Caused by: org.springframework.messaging.converter.MessageConversionException: Cannot convert from [[B]

 

Solution:

This is caused by the MQ converter not using Jackson yet. So just add following piece to solve this issue:

@SpringBootApplication
@EnableRabbit

public class MYCloudApplication {

@Bean
 public MessageConverter jsonMessageConverter() {
 return new Jackson2JsonMessageConverter();
 }

RabbitMQ quick start for Spring Boot

  1. Install the rabbitmq
sudo apt-get update
sudo apt-get install rabbitmq-server
sudo service rabbitmq-server stop
sudo service rabbitmq-server start
sudo service rabbitmq-server status
sudo rabbitmqctl status

cat /proc/$RABBITMQ_BEAM_PROCESS_PID/limits

The broker always appends to the log files, so a complete log history is retained.

/var/log/rabbitmq directory.
See /etc/logrotate.d/rabbitmq-server to configure logrotate.

The rabbbitMQ will run at port localhost:5672

 

2. Let us build a Spring Boot project to make MQ work.

2.1  Download the spring boot sample project from https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-amqp.

remember to change pom.xml parent as this:

 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.5.10.RELEASE</version>
 <relativePath /> <!-- lookup parent from repository -->
 </parent>

 

2.2 Use eclipse to import this maven project. and use “java -jar” run it, you will see it continuously say hello.

2.3 The code use following tricks:

a. @EnableScheduling and @Scheduled(fixedDelay = 1000L)  will make the app auto start a Schedule task every 1 second and call the send() method to send a msg “hello” to the MQ queue “foo”:

@RabbitListener(queues = "foo")

defines a mq listener to listen to the queue of “foo”.

b. When a msg is in the queue, this method

@RabbitHandler
 public void process(@Payload String foo) { ...}

will be triggered to process the msg got in this queue, code just prints it our in stdout.

c. the method fooQueue must names as the fooQueue as the queue name in this example is “foo”.

public Queue fooQueue() {...}

d. This code:

 @Autowired
 private RabbitTemplate rabbitTemplate;

Will auto get a connection from the RabbitMQ so it can do the send later for you.

 

3. Access RabbitMQ GUI to make simple management

Enable the rabbitmq_management module:

sudo rabbitmq-plugins enable rabbitmq_management

The Web UI is located at: http://server-name:15672/
The user “guest” is created with password “guest” by default.