Spring Web Services এবং Asynchronous Communication

স্প্রিং ওয়েব সার্ভিসেস (Spring Web Services) - Java Technologies

273

Spring Web Services একটি শক্তিশালী ফ্রেমওয়ার্ক যা SOAP এবং RESTful ওয়েব সার্ভিস তৈরির জন্য ব্যবহৃত হয়। অ্যাসিনক্রোনাস কমিউনিকেশন (Asynchronous Communication) একটি গুরুত্বপূর্ণ বৈশিষ্ট্য, যা ওয়েব সার্ভিসের পারফরম্যান্স বাড়ায় এবং দীর্ঘ-চলমান কাজের জন্য সার্ভার-সাইড স্কেলিং সহজ করে।


Asynchronous Communication কী?

Asynchronous Communication হলো এমন একটি পদ্ধতি যেখানে ক্লায়েন্ট সার্ভারের সাথে যোগাযোগ করে এবং কাজটি সম্পন্ন হওয়ার জন্য অপেক্ষা না করেই অন্য কাজ শুরু করতে পারে।
এই পদ্ধতি বিশেষত নিম্নলিখিত ক্ষেত্রে কার্যকর:

  1. দীর্ঘ-চলমান প্রসেসিং।
  2. ব্যাকগ্রাউন্ড টাস্ক।
  3. ম্যাসেজ কিউয়ের মাধ্যমে ডেটা প্রসেসিং।

Spring Web Services-এ Asynchronous Communication এর ব্যবহার

Spring-এ Asynchronous Communication বিভিন্নভাবে পরিচালনা করা যায়। সাধারণ পদ্ধতি হলো:

  1. Spring Async Support
    অ্যানোটেশন ভিত্তিক অ্যাসিনক্রোনাস প্রসেসিং।
  2. JMS (Java Message Service)
    ম্যাসেজ কিউ বা টপিকের মাধ্যমে অ্যাসিনক্রোনাস ডেলিভারি।
  3. DeferredResult বা CompletableFuture
    ক্লায়েন্ট এবং সার্ভারের মধ্যে নন-ব্লকিং কল ব্যবহারের জন্য।

উদাহরণ: Spring Async Support ব্যবহার করে SOAP Web Service

১. Maven Dependency যোগ করা

pom.xml-এ ডিপেনডেন্সি যুক্ত করুন:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

২. Async Configuration

Spring অ্যাসিঙ্ক সক্ষম করতে কনফিগার করুন:

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

@Configuration
@EnableAsync
public class AsyncConfig {
    // Custom Async Executor (optional)
}

৩. Asynchronous Service তৈরি

SOAP Web Service-এ অ্যাসিঙ্ক প্রসেসিং যুক্ত করুন:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncService {

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

    @Async
    public void processAsyncTask(String data) {
        logger.info("Processing async task: {}", data);
        try {
            // Simulating a long-running task
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            logger.error("Error during async processing", e);
        }
        logger.info("Async task completed");
    }
}

৪. SOAP Endpoint তৈরি

SOAP Endpoint-এ অ্যাসিঙ্ক সাপোর্ট যুক্ত করুন:

import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

@Endpoint
public class AsyncEndpoint {

    private static final String NAMESPACE_URI = "http://example.com/soap";

    private final AsyncService asyncService;

    public AsyncEndpoint(AsyncService asyncService) {
        this.asyncService = asyncService;
    }

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "AsyncRequest")
    @ResponsePayload
    public String handleAsyncRequest(@RequestPayload String request) {
        asyncService.processAsyncTask(request); // Process asynchronously
        return "Request received. Processing asynchronously.";
    }
}

উদাহরণ: CompletableFuture ব্যবহার করে RESTful Web Service

১. REST Controller তৈরি

CompletableFuture দিয়ে নন-ব্লকিং অ্যাসিঙ্ক প্রসেসিং:

import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

@RestController
public class AsyncController {

    @GetMapping("/async-task")
    @Async
    public CompletableFuture<String> performAsyncTask() {
        return CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000); // Simulate long task
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Task completed asynchronously!";
        });
    }
}

JMS ব্যবহার করে Asynchronous Communication

Spring Web Services-এ অ্যাসিঙ্ক প্রসেসিং JMS এর মাধ্যমে করা যায়। উদাহরণ:

  1. ActiveMQ বা RabbitMQ ব্যবহার।
  2. ম্যাসেজ পাঠানো ও গ্রহণ করার জন্য Spring JMS টেমপ্লেট ব্যবহার।

Spring JMS উদাহরণ

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

@Service
public class JmsAsyncService {

    @JmsListener(destination = "asyncQueue")
    public void receiveMessage(String message) {
        System.out.println("Received async message: " + message);
    }
}

উপসংহার

Spring Web Services-এ Asynchronous Communication বিভিন্নভাবে বাস্তবায়ন করা যায়।
SOAP বা RESTful ওয়েব সার্ভিসে অ্যাসিঙ্ক প্রসেসিং ব্যবহার করে আপনি:

  1. অ্যাপ্লিকেশনের পারফরম্যান্স বাড়াতে পারবেন।
  2. লোড এবং স্কেলিং ম্যানেজ করতে পারবেন।

আপনার নির্দিষ্ট প্রয়োজন অনুযায়ী উদাহরণ বা কনফিগারেশনে সাহায্য করতে আমি প্রস্তুত!

Content added By

Spring Web Services (Spring-WS) এ Asynchronous Web Services এমন একটি কৌশল যেখানে ক্লায়েন্ট এবং সার্ভার একই সময়ে একে অপরের সাথে যোগাযোগ শেষ করতে বাধ্য হয় না। এই পদ্ধতিতে, ক্লায়েন্ট একটি অনুরোধ পাঠানোর পরে সার্ভারের প্রতিক্রিয়া আসার জন্য অপেক্ষা না করে কাজ চালিয়ে যেতে পারে। সার্ভার পরে কোনো একটি চ্যানেলের মাধ্যমে ক্লায়েন্টকে প্রতিক্রিয়া পাঠিয়ে দেয়।

Asynchronous Web Services-এর ধারণা

  1. Synchronous vs Asynchronous Communication:
    • Synchronous: ক্লায়েন্ট সার্ভারের প্রতিক্রিয়ার জন্য অপেক্ষা করে।
    • Asynchronous: ক্লায়েন্ট প্রতিক্রিয়ার জন্য অপেক্ষা করে না; প্রতিক্রিয়া পরে সরবরাহ করা হয়।
  2. Spring Web Services-এ Asynchronous ব্যবহারের সুবিধা:
    • উচ্চ পারফরম্যান্স: ক্লায়েন্ট অপেক্ষা না করায় আরও বেশি কাজ সমান্তরালে করা যায়।
    • রিসোর্স সাশ্রয়: থ্রেড বা কানেকশন দীর্ঘ সময় ধরে আটকে থাকে না।
    • স্কেলেবিলিটি: একাধিক অনুরোধ পরিচালনা করা সহজ হয়।

Asynchronous Web Services বাস্তবায়নের পদ্ধতি

Spring Framework-এ Asynchronous Web Services তৈরি করতে আপনি কয়েকটি পদ্ধতি অনুসরণ করতে পারেন।


1. Spring Async Rest Template (Non-SOAP Based):

যদিও Spring-WS মূলত SOAP-এর জন্য ব্যবহৃত হয়, যদি SOAP মেসেজ প্রক্রিয়াকরণ দরকার না হয়, তাহলে আপনি AsyncRestTemplate ব্যবহার করতে পারেন। এটি HTTP ভিত্তিক অ্যাসিঙ্ক্রোনাস যোগাযোগের জন্য ব্যবহার করা হয়।

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.AsyncRestTemplate;
import java.util.concurrent.CompletableFuture;

public class AsyncServiceClient {
    private final AsyncRestTemplate restTemplate = new AsyncRestTemplate();

    public CompletableFuture<ResponseEntity<String>> callAsyncService(String url) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                return restTemplate.getForEntity(url, String.class).get();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }
}

2. Spring WebFlux (Reactive Programming):

Spring WebFlux SOAP বা RESTful ওয়েব সার্ভিসের জন্য রিঅ্যাক্টিভ পদ্ধতি প্রদান করে। এটি একটি সম্পূর্ণরূপে নন-ব্লকিং এবং অ্যাসিঙ্ক্রোনাস মডেল।

import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

public class ReactiveServiceClient {
    private final WebClient webClient = WebClient.create();

    public Mono<String> callReactiveService(String url) {
        return webClient.get()
                .uri(url)
                .retrieve()
                .bodyToMono(String.class);
    }
}

3. SOAP-Based Asynchronous Web Services:

SOAP এর ক্ষেত্রে Spring-WS-এ অ্যাসিঙ্ক্রোনাস মেসেজ প্রসেসিং করার জন্য JMS (Java Messaging Service) বা Spring Integration ব্যবহার করা যেতে পারে।

JMS ব্যবহার করে Asynchronous SOAP Communication:
  1. ক্লায়েন্ট একটি SOAP মেসেজকে JMS Queue-তে পাঠায়।
  2. সার্ভার মেসেজটি প্রসেস করে এবং ফলাফলকে আরেকটি Queue-তে পাঠিয়ে দেয়।
  3. ক্লায়েন্ট ফলাফলটি Queue থেকে গ্রহণ করে।

Spring Configuration উদাহরণ:

JMS Listener:

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class SoapMessageListener {

    @JmsListener(destination = "requestQueue")
    public void processMessage(String soapRequest) {
        // SOAP মেসেজ প্রসেস করুন
        System.out.println("Processing SOAP Request: " + soapRequest);
        // Response Queue-তে ফলাফল পাঠান
    }
}

JMS Template (SOAP Request পাঠানো):

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

@Service
public class SoapMessageSender {
    private final JmsTemplate jmsTemplate;

    public SoapMessageSender(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    public void sendMessage(String soapMessage) {
        jmsTemplate.convertAndSend("requestQueue", soapMessage);
    }
}

4. Callback এবং Future ব্যবহার:

Spring Async এর সাহায্যে @Async অ্যানোটেশন ব্যবহার করে অ্যাসিঙ্ক্রোনাস প্রক্রিয়া বাস্তবায়ন করা যেতে পারে।

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

@Service
public class AsyncSoapService {

    @Async
    public Future<String> processSoapRequest(String request) {
        // SOAP মেসেজ প্রসেস করুন
        return new AsyncResult<>("Processed: " + request);
    }
}

Asynchronous Web Services-এর প্রধান চ্যালেঞ্জ:

  1. Error Handling: অ্যাসিঙ্ক্রোনাস প্রক্রিয়ায় ত্রুটি পরিচালনা করা কিছুটা জটিল।
  2. Callback Overhead: অনেক বেশি কলব্যাক ব্যবস্থাপনা জটিল করে তুলতে পারে।
  3. State Management: ক্লায়েন্ট এবং সার্ভারের মধ্যে স্টেট ধরে রাখা কঠিন।

ব্যবহার ক্ষেত্র:

  1. High-Volume Processing: যেখানে হাজার হাজার অনুরোধ দ্রুত প্রক্রিয়াজাত করতে হয়।
  2. Time-Consuming Operations: দীর্ঘ সময় লাগা কাজগুলোকে ব্যাকগ্রাউন্ডে পরিচালনা করা যায়।
  3. Integration with Legacy Systems: যেখানে মেসেজিং চ্যানেল বা Queue ব্যবহারের প্রয়োজন।

Spring Web Services-এ Asynchronous পদ্ধতি কার্যকরীভাবে ব্যবহার করলে আপনি একটি স্কেলেবল এবং রেসপন্সিভ সিস্টেম তৈরি করতে পারবেন।

Content added By

@Async অ্যানোটেশন কী?

স্প্রিং ফ্রেমওয়ার্কে @Async অ্যানোটেশনটি ব্যবহার করা হয় কোনো মেথড বা টাস্ককে অ্যাসিঙ্ক্রোনাসলি (asynchronously) চালানোর জন্য। এটি থ্রেড ব্লক না করে ব্যাকগ্রাউন্ডে একটি আলাদা থ্রেড ব্যবহার করে কাজ সম্পন্ন করে।

অ্যাসিঙ্ক্রোনাস প্রসেসিংয়ের মূল লক্ষ্য হলো অ্যাপ্লিকেশনের কার্যকারিতা বৃদ্ধি করা এবং মেইন থ্রেডকে ব্লক না করে দ্রুত প্রতিক্রিয়া প্রদান করা।


@Async এর কাজ করার পদ্ধতি

  1. মেথড আলাদাভাবে চালানো: @Async দিয়ে চিহ্নিত মেথডটি ডিফল্ট থ্রেড থেকে আলাদা একটি থ্রেড পুলে রান করে।
  2. অ্যাসিঙ্ক্রোনাস প্রসেসিং: অ্যাপ্লিকেশনটি মেথড চালানোর সময় ব্লক না হয়ে কাজ চালিয়ে যেতে পারে।
  3. রিটার্ন টাইপ:
    • যদি মেথড কিছু রিটার্ন না করে, তখন এটি void হতে পারে।
    • যদি মেথড কিছু রিটার্ন করে, তখন এটি Future বা CompletableFuture হতে পারে।

@Async ব্যবহার করার ধাপ

1. স্প্রিং কনফিগারেশন এ @EnableAsync যোগ করা

@Async কাজ করার জন্য ক্লাস বা কনফিগারেশন ফাইলে @EnableAsync যোগ করতে হবে।

@Configuration
@EnableAsync
public class AppConfig {
    // Additional configurations if needed
}

2. @Async দিয়ে মেথড ডিফাইন করা

একটি মেথডকে অ্যাসিঙ্ক্রোনাস হিসেবে ডিফাইন করতে @Async ব্যবহার করতে হবে।

@Service
public class MyService {

    @Async
    public void asyncMethod() {
        System.out.println("Executing async method: " + Thread.currentThread().getName());
        try {
            Thread.sleep(2000); // Simulate delay
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Async method execution complete.");
    }

    @Async
    public CompletableFuture<String> asyncMethodWithReturn() {
        System.out.println("Executing async method with return: " + Thread.currentThread().getName());
        try {
            Thread.sleep(2000); // Simulate delay
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return CompletableFuture.completedFuture("Task Completed!");
    }
}

3. মেথড কল করা

@Async দিয়ে চিহ্নিত মেথড কল করার সময় এটি আলাদা থ্রেডে চালু হয়।

@RestController
public class MyController {

    private final MyService myService;

    public MyController(MyService myService) {
        this.myService = myService;
    }

    @GetMapping("/executeAsync")
    public String executeAsyncTask() {
        myService.asyncMethod();
        return "Async Task Executed!";
    }

    @GetMapping("/executeAsyncWithReturn")
    public CompletableFuture<String> executeAsyncTaskWithReturn() {
        return myService.asyncMethodWithReturn();
    }
}

বিকল্প থ্রেড পুল কনফিগার করা

ডিফল্ট থ্রেড পুলের পরিবর্তে কাস্টম থ্রেড পুল ব্যবহার করতে হলে কনফিগারেশন যোগ করতে হবে।

@Configuration
@EnableAsync
public class AsyncConfig {

    @Bean(name = "taskExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(25);
        executor.setThreadNamePrefix("AsyncThread-");
        executor.initialize();
        return executor;
    }
}

@Async ব্যবহার করার সুবিধা

  1. পারফরম্যান্স বৃদ্ধি: ব্যাকগ্রাউন্ড থ্রেড ব্যবহার করে দ্রুত কাজ সম্পন্ন করা।
  2. ইউজার এক্সপেরিয়েন্স উন্নত করা: দীর্ঘমেয়াদী টাস্ক আলাদা থ্রেডে প্রসেস করে মেইন অ্যাপ্লিকেশনকে দ্রুত প্রতিক্রিয়া দেওয়ার সুযোগ।
  3. রিসোর্সের কার্যকর ব্যবহার: মেইন থ্রেড ব্লক না করে অ্যাপ্লিকেশন মসৃণভাবে চালানো যায়।

মনে রাখার বিষয়সমূহ

  1. @Async কাজ করার জন্য স্প্রিং কনটেইনারের (Spring Context) মাধ্যমে ক্লাসটি ম্যানেজ হতে হবে। অর্থাৎ, এটি একটি @Service, @Component, বা @Bean হতে হবে।
  2. @Async-এর কাজ সঠিকভাবে সম্পন্ন করতে হলে থ্রেড পুল ঠিকভাবে কনফিগার করা জরুরি।
  3. অ্যাসিঙ্ক্রোনাস মেথডগুলোর রিটার্ন টাইপ void, Future, বা CompletableFuture হতে হবে।

উপসংহার

স্প্রিং-এর @Async অ্যানোটেশন দীর্ঘমেয়াদী প্রসেসিংয়ের জন্য একটি কার্যকর এবং সহজ উপায়। এটি অ্যাপ্লিকেশনের কার্যক্ষমতা বৃদ্ধি করে এবং ব্যবহারকারীর অভিজ্ঞতা উন্নত করতে সহায়ক।

Content added By

Spring Web Services-এ DeferredResult এবং CompletableFuture ব্যবহার করে Asynchronous Processing প্রয়োগ করা হয়, যা ক্লায়েন্টের অনুরোধ এবং সার্ভারের প্রতিক্রিয়া পরিচালনা করার সময় অ্যাপ্লিকেশনটিকে ব্লক না করে উচ্চ পারফরম্যান্স প্রদান করে।

Asynchronous Processing: DeferredResult এবং CompletableFuture

DeferredResult:

DeferredResult হল Spring MVC-তে একটি Async প্রসেসিং টুল, যা মূল থ্রেডকে ব্লক না করে একটি দীর্ঘমেয়াদি অনুরোধের প্রতিক্রিয়া পাঠাতে দেয়।

উদাহরণ:

import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.ForkJoinPool;

@RestController
@RequestMapping("/async")
public class AsyncController {

    @GetMapping("/deferred")
    public DeferredResult<String> handleAsyncRequest() {
        // DeferredResult তৈরি করুন
        DeferredResult<String> deferredResult = new DeferredResult<>(5000L, "Timeout");
        
        // Async প্রসেস শুরু করুন
        ForkJoinPool.commonPool().submit(() -> {
            try {
                Thread.sleep(3000); // Mock delay
                deferredResult.setResult("DeferredResult Response");
            } catch (InterruptedException e) {
                deferredResult.setErrorResult("Error occurred");
            }
        });

        return deferredResult;
    }
}

বৈশিষ্ট্য:

  1. Async প্রসেস চলাকালীন মূল থ্রেড মুক্ত থাকে।
  2. সময়মতো সাড়া না দিলে ডিফল্ট মেসেজ পাঠানো যায়।

CompletableFuture:

CompletableFuture হল Java 8-এ আসা একটি ক্লাস, যা অ্যাসিঙ্ক্রোনাস কম্পিউটেশন পরিচালনা করতে ব্যবহৃত হয়।

উদাহরণ:

import org.springframework.web.bind.annotation.*;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/async")
public class AsyncController {

    @GetMapping("/future")
    public CompletableFuture<String> handleFutureRequest() {
        // CompletableFuture তৈরি করুন
        return CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000); // Mock delay
                return "CompletableFuture Response";
            } catch (InterruptedException e) {
                throw new RuntimeException("Error occurred");
            }
        });
    }
}

বৈশিষ্ট্য:

  1. সহজ Async প্রসেসিং।
  2. চেইনিং এবং একাধিক টাস্ক একত্রে পরিচালনা করার জন্য আদর্শ।

DeferredResult এবং CompletableFuture-এর তুলনা

বৈশিষ্ট্যDeferredResultCompletableFuture
Spring IntegrationSpring MVC-এর জন্য নেটিভ সাপোর্টJava Standard API
Callback Handlingসরাসরি কাস্টম থ্রেডে প্রসেস করা যায়চেইনিং এবং ফিউচার ম্যানিপুলেশন সহজ
Timeout Supportডিফল্ট টাইমআউট এবং প্রতিক্রিয়া সেট করা যায়টাইমআউট পরিচালনার জন্য কাস্টম লজিক প্রয়োজন

Server Side Configuration:

Spring-এ Async প্রসেসিং সাপোর্ট সক্রিয় করতে @EnableAsync এবং AsyncConfigurer ব্যবহার করুন।

উদাহরণ:

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class AsyncConfig {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(25);
        executor.setThreadNamePrefix("AsyncExecutor-");
        executor.initialize();
        return executor;
    }
}

ক্লায়েন্টের উদাহরণ (RestTemplate):

import org.springframework.web.client.RestTemplate;
import java.util.concurrent.CompletableFuture;

public class AsyncClient {

    private RestTemplate restTemplate = new RestTemplate();

    public CompletableFuture<String> callAsyncEndpoint() {
        return CompletableFuture.supplyAsync(() -> 
            restTemplate.getForObject("http://localhost:8080/async/future", String.class)
        );
    }
}

ব্যবহারের ক্ষেত্রে

  1. লং-রানিং টাস্ক: যেমন ডেটা প্রক্রিয়াজাতকরণ বা এক্সটার্নাল API কল।
  2. রিসোর্স অপ্টিমাইজেশন: মূল থ্রেডকে ব্লক না করে অন্য কাজ করতে দেওয়া।
  3. রিয়েল-টাইম সিস্টেম: যেখানে তাত্ক্ষণিক প্রতিক্রিয়া প্রয়োজন।

মূল সুবিধা

  • উচ্চ পারফরম্যান্স।
  • টাইমআউট বা ফল্ট হ্যান্ডলিং সহজতর।
  • Scalability বৃদ্ধির জন্য কার্যকর।
Content added By

স্প্রিং ওয়েব সার্ভিসে Asynchronous Communication একটি শক্তিশালী বৈশিষ্ট্য যা সিস্টেমের স্কেল এবং কর্মক্ষমতা বৃদ্ধি করতে সাহায্য করে। এটি ব্যবহার করে আপনি ওয়েব সার্ভিসে ক্লায়েন্ট এবং সার্ভারের মধ্যে সমান্তরাল যোগাযোগ সেট আপ করতে পারেন।

নিচে স্প্রিং ওয়েব সার্ভিসে Asynchronous Communication সেটআপ করার উদাহরণসহ প্রক্রিয়া দেওয়া হলো:


Asynchronous Communication এর প্রক্রিয়া

  1. Client-Side:
    ক্লায়েন্ট অনুরোধ প্রেরণ করে এবং প্রতিক্রিয়ার জন্য অপেক্ষা না করে তার কাজ চালিয়ে যায়। যখন প্রতিক্রিয়া আসে, একটি কলব্যাক (callback) বা প্রমিজ (promise) হ্যান্ডলিং করা হয়।
  2. Server-Side:
    সার্ভার ধীরগতির কাজ (যেমন: ডেটাবেস কুয়েরি, এক্সটার্নাল API কল) করতে থাকলেও ক্লায়েন্টকে অবহিত করে যে অনুরোধটি গ্রহণ করা হয়েছে এবং এটি প্রক্রিয়াধীন।

উদাহরণ: Spring Web Services ব্যবহার করে Asynchronous Communication

১. প্রয়োজনীয় নির্ভরতা (Dependencies)

<dependencies>
    <!-- Spring Web Services -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-ws-core</artifactId>
    </dependency>

    <!-- Async Support -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
</dependencies>

২. Server-Side Implementation

Controller Setup

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import java.util.concurrent.CompletableFuture;

@Endpoint
public class AsyncEndpoint {

    private static final String NAMESPACE_URI = "http://example.com/async";

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "AsyncRequest")
    @ResponsePayload
    @Async // Marks the method to be executed asynchronously
    public CompletableFuture<AsyncResponse> handleAsyncRequest(@RequestPayload AsyncRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            AsyncResponse response = new AsyncResponse();
            response.setMessage("Processing request for: " + request.getName());
            try {
                // Simulate processing delay
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            response.setStatus("Completed");
            return response;
        });
    }
}

Async Configuration

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

@Configuration
@EnableAsync
public class AsyncConfig {
    // Additional configurations can be added if necessary
}

৩. Client-Side Implementation

Asynchronous Web Service Template

import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.scheduling.annotation.Async;
import org.springframework.ws.client.core.WebServiceTemplate;

import java.util.concurrent.CompletableFuture;

public class AsyncClient {

    private WebServiceTemplate webServiceTemplate;

    public AsyncClient() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.example.async");
        this.webServiceTemplate = new WebServiceTemplate(marshaller);
    }

    @Async
    public CompletableFuture<AsyncResponse> sendAsyncRequest(AsyncRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            return (AsyncResponse) webServiceTemplate.marshalSendAndReceive("http://localhost:8080/ws", request);
        });
    }
}

৪. XML Configuration

Request XML:

<AsyncRequest xmlns="http://example.com/async">
    <name>John Doe</name>
</AsyncRequest>

Response XML:

<AsyncResponse xmlns="http://example.com/async">
    <message>Processing request for: John Doe</message>
    <status>Completed</status>
</AsyncResponse>

৫. Spring Boot Application

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AsyncWebServiceApplication {

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

কিভাবে এটি কাজ করে?

  1. Client Request:
    ক্লায়েন্ট sendAsyncRequest মেথড ব্যবহার করে একটি অনুরোধ পাঠায়। এটি asynchronous হওয়ায় মেইন থ্রেড ব্লক হয় না।
  2. Server Processing:
    সার্ভার handleAsyncRequest মেথডে অনুরোধ গ্রহণ করে এবং একটি নতুন থ্রেডে কাজ সম্পন্ন করে।
  3. Response Handling:
    ক্লায়েন্ট যখন সার্ভার থেকে প্রতিক্রিয়া পায়, তখন এটি একটি callback বা CompletableFuture এর মাধ্যমে হ্যান্ডেল হয়।

সুবিধাসমূহ:

  • উচ্চ পারফরম্যান্স:
    সার্ভারের থ্রেড ব্লক না হওয়ায় বেশি সংখ্যক অনুরোধ একসাথে প্রক্রিয়া করা যায়।
  • রেসপন্সিভ অ্যাপ্লিকেশন:
    ক্লায়েন্ট সাইড অ্যাপ্লিকেশন প্রতিক্রিয়াশীল থাকে কারণ এটি অনুরোধের জন্য অপেক্ষা করে না।
  • স্কেলযোগ্যতা:
    অ্যাসিনক্রোনাস যোগাযোগ সিস্টেমের ওভারহেড কমায় এবং স্কেলিং সহজ করে।

উন্নত ব্যবহার

  • RabbitMQ বা Kafka এর মতো মেসেজ কিউ ব্যবহার করে আরও কার্যকর Asynchronous Communication গঠন করা যায়।
  • Spring Integration বা Spring Cloud Streams এর মতো টুল ব্যবহার করে মেসেজিং সহজ করা যায়।
Content added By
Promotion

Are you sure to start over?

Loading...