Tuesday, July 11, 2023

How To Use Threads In Spring Boot Rest Api

In this example you will learn, How to use multithreading in spring boot application using restful services.

Suppose you have two api one for push messages and other is for receive message.

But you want. if we hit push message api first then you want to wait to complete push message api first.

in mean while if you hit receive message api before complete push api. So you want to wait receive api until push api completed.

you want to use methods In below service using threads.  

MessageServiceImpl.java
package in.javaiq.service;

import org.springframework.stereotype.Service;

@Service
public class MessageServiceImpl implements MessageService {

	private static volatile String threadStatus = "False";
	
	@Override
	public String getThreadStatus() {
		return this.threadStatus;
	}

	@Override
	public String sendMessage() {
		this.threadStatus = "true";
		for (int i = 0; i < 1000000; i++) {
			System.out.println("##############  Push Notification " + i + " ###############");
		}
		this.threadStatus = "false";
		System.out.println(" #### ThreadStatus ####" + threadStatus);
		return this.threadStatus;
	}

	@Override
	public String getMessageResponce() {

		for (int i = 0; i < 1000000; i++) {
			System.out.println("##############  Recived " + i + " ###############");
		}

		return "Success";
	}
}
Create Two Threads - PushThread and ResponceThread
PushThread.java
package in.javaiq.service;

import org.springframework.beans.factory.annotation.Autowired;

public class PushThread extends Thread {

	String status;
	@Autowired
	private MessageService messageService;

	PushThread(MessageService messageService) {
		this.messageService = messageService;
	}

	@Override
	public void run() {
		synchronized (messageService) {
			String status = this.messageService.sendMessage();
			if (messageService.getThreadStatus().equalsIgnoreCase("False")) {
				messageService.notifyAll();
			}
		}
	}
}
ResponceThread.java
package in.javaiq.service;

import org.springframework.beans.factory.annotation.Autowired;

public class ResponceThread extends Thread {
	String status;
	@Autowired
	private MessageService messageService;

	ResponceThread(MessageService messageService) {
		this.messageService = messageService;
	}

	@Override
	public void run() {
		synchronized (messageService) {
			try {
				if (messageService.getThreadStatus().equalsIgnoreCase("true")) {
					messageService.wait();
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("############ ResponceThread ##############");
			String status = this.messageService.getMessageResponce();
		}
	}

}
Create Service -
CommonService.java
package in.javaiq.service;

public interface CommonService {
	
	public String pushMessage() throws InterruptedException;

	public String messageResponce() throws InterruptedException;

}
CommonServiceImpl.java
package in.javaiq.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommonServiceImpl implements CommonService {

	@Autowired
	private MessageService messageService;

	@Override
	public String pushMessage() throws InterruptedException {
		PushThread pushThread = new PushThread(messageService);
		pushThread.start();

		return pushThread.status;
	}

	@Override
	public String messageResponce() throws InterruptedException {
		ResponceThread responceThread = new ResponceThread(messageService);
		responceThread.start();
		return responceThread.status;
	}

}
MessageController.java
package in.javaiq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import in.javaiq.service.CommonService;

@RestController
@RequestMapping("/message")
public class MessageController {

	@Autowired
	private CommonService commonService;

	@GetMapping(path = "/push")
	public String pushNotification() throws InterruptedException {
		return commonService.pushMessage();
	}

	@GetMapping(path = "/responce")
	public String messageResponse() throws InterruptedException {
		return commonService.messageResponce();
	}
}
In the above example you can see, how to use threads in Spring Boot Restful Api.

No comments:

Post a Comment