উদাহরণ সহ Asynchronous Communication

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

227

স্প্রিং ওয়েব সার্ভিসে 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...