Java NIO (Non-blocking I/O) এর মাধ্যমে আমরা এমন অপারেশন করতে পারি যা সিস্টেমের পারফরম্যান্স বাড়ায় এবং ডেটা প্রসেসিংয়ের গতি উন্নত করে। Asynchronous I/O (এএসিঙ্ক্রোনাস I/O) একটি শক্তিশালী পদ্ধতি, যা I/O অপারেশনগুলিকে ব্লক না করে একসাথে একাধিক কাজ করতে সক্ষম করে। AsynchronousFileChannel এবং AsynchronousSocketChannel হল দুটি প্রধান ক্লাস যা Java NIO এ Asynchronous I/O অপারেশন পরিচালনা করতে ব্যবহৃত হয়।
এই লেখায়, আমরা দেখব কিভাবে AsynchronousFileChannel এবং AsynchronousSocketChannel ব্যবহার করা যায় এবং তাদের বিভিন্ন বাস্তবায়ন।
১. AsynchronousFileChannel
AsynchronousFileChannel Java NIO এর একটি ক্লাস যা ফাইলের উপর Asynchronous I/O অপারেশন পরিচালনা করতে ব্যবহৃত হয়। এটি ফাইল থেকে ডেটা পড়া, লেখা এবং ফাইলের অবস্থান পরিবর্তন করার জন্য ব্যবহৃত হয়, এবং এটি non-blocking I/O অপারেশনগুলো করতে সক্ষম।
AsynchronousFileChannel এর ব্যবহার
- এটি non-blocking ফাইল I/O অপারেশন সম্পাদন করে, অর্থাৎ ফাইলের ডেটা পড়া বা লেখা হওয়ার সময় সিস্টেমের অন্যান্য কাজ ব্লক হয় না।
- এটি Callback Mechanism ব্যবহার করে অপারেশন সম্পন্ন হলে ফলাফল প্রদান করে।
উদাহরণ: AsynchronousFileChannel দিয়ে ফাইল লেখা
import java.nio.file.*;
import java.nio.channels.*;
import java.nio.ByteBuffer;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class AsynchronousFileWriteExample {
public static void main(String[] args) throws IOException {
Path path = Paths.get("async_example.txt");
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
String content = "This is an asynchronous file write example.";
ByteBuffer buffer = ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8));
fileChannel.write(buffer, 0, null, new CompletionHandler<Integer, Void>() {
@Override
public void completed(Integer result, Void attachment) {
System.out.println("Data written successfully!");
}
@Override
public void failed(Throwable exc, Void attachment) {
exc.printStackTrace();
}
});
// Block the main thread until the operation is completed
try {
Thread.sleep(2000); // Waiting for the asynchronous operation to complete
} catch (InterruptedException e) {
e.printStackTrace();
}
fileChannel.close();
}
}
এখানে, AsynchronousFileChannel.write() ব্যবহার করে ফাইলের মধ্যে ডেটা লেখা হয়েছে। লেখার অপারেশন non-blocking এবং callback দ্বারা পরিচালিত হয়েছে, যেখানে completed() মেথড সফল অপারেশন শেষ হলে কল হয় এবং failed() মেথড যদি কোনো সমস্যা ঘটে।
২. AsynchronousSocketChannel
AsynchronousSocketChannel একটি নেটওয়ার্ক চ্যানেল যা TCP/IP স্যকেট কমিউনিকেশন পরিচালনা করতে ব্যবহৃত হয়। এটি non-blocking এবং asynchronous I/O অপারেশন পরিচালনা করতে সক্ষম, যার মাধ্যমে ডেটা পাঠানো এবং গ্রহণ করার সময় অন্যান্য কাজ সিস্টেমের অন্য অংশে ব্লক না করে চলতে থাকে।
AsynchronousSocketChannel এর ব্যবহার
- এটি non-blocking TCP/IP কানেকশন এবং ডেটা ট্রান্সফার পরিচালনা করে।
- এটি callback functions এর মাধ্যমে অপারেশন সম্পন্ন হলে ফলাফল প্রদান করে।
- এটি সিস্টেমের কার্যক্ষমতা বাড়ায়, কারণ একাধিক নেটওয়ার্ক কনেকশন বা I/O অপারেশন একসাথে প্রক্রিয়া করা যায়।
উদাহরণ: AsynchronousSocketChannel দিয়ে সার্ভার এবং ক্লায়েন্ট
ক্লায়েন্ট (AsynchronousSocketChannel ব্যবহার করে)
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.io.IOException;
public class AsynchronousClientExample {
public static void main(String[] args) throws IOException {
AsynchronousSocketChannel clientChannel = AsynchronousSocketChannel.open();
clientChannel.connect(new InetSocketAddress("localhost", 8080), null, new CompletionHandler<Void, Void>() {
@Override
public void completed(Void result, Void attachment) {
String message = "Hello from client!";
ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
clientChannel.write(buffer, null, new CompletionHandler<Integer, Void>() {
@Override
public void completed(Integer result, Void attachment) {
System.out.println("Message sent to server!");
}
@Override
public void failed(Throwable exc, Void attachment) {
exc.printStackTrace();
}
});
}
@Override
public void failed(Throwable exc, Void attachment) {
exc.printStackTrace();
}
});
// Block the main thread until the operation is completed
try {
Thread.sleep(2000); // Waiting for the asynchronous operation to complete
} catch (InterruptedException e) {
e.printStackTrace();
}
clientChannel.close();
}
}
সার্ভার (AsynchronousServerSocketChannel ব্যবহার করে)
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.io.IOException;
public class AsynchronousServerExample {
public static void main(String[] args) throws IOException {
AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(8080));
serverChannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {
@Override
public void completed(AsynchronousSocketChannel clientChannel, Void attachment) {
try {
System.out.println("Client connected.");
ByteBuffer buffer = ByteBuffer.allocate(1024);
clientChannel.read(buffer, null, new CompletionHandler<Integer, Void>() {
@Override
public void completed(Integer result, Void attachment) {
buffer.flip();
String message = new String(buffer.array(), StandardCharsets.UTF_8).trim();
System.out.println("Received from client: " + message);
}
@Override
public void failed(Throwable exc, Void attachment) {
exc.printStackTrace();
}
});
serverChannel.accept(null, this); // Accept next client connection
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void failed(Throwable exc, Void attachment) {
exc.printStackTrace();
}
});
// Block the main thread indefinitely, server is running
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
এখানে, AsynchronousSocketChannel ব্যবহার করে ক্লায়েন্ট এবং সার্ভার তৈরি করা হয়েছে। ক্লায়েন্ট সার্ভারে একটি মেসেজ পাঠাচ্ছে এবং সার্ভার সেই মেসেজ গ্রহণ করছে। সার্ভার এবং ক্লায়েন্ট উভয়ই non-blocking মোডে কাজ করছে, অর্থাৎ তারা I/O অপারেশন প্রক্রিয়া করতে অন্যান্য কাজের জন্য ব্লক হবে না।
Java NIO এর AsynchronousFileChannel এবং AsynchronousSocketChannel দুটি গুরুত্বপূর্ণ কম্পোনেন্ট যা non-blocking এবং asynchronous I/O অপারেশন করতে সক্ষম।
- AsynchronousFileChannel ফাইল I/O অপারেশনগুলিকে non-blocking এবং asynchronous করে, যা পারফরম্যান্স বাড়ায়।
- AsynchronousSocketChannel TCP/IP কানেকশন পরিচালনা করে asynchronous নেটওয়ার্ক যোগাযোগের জন্য।
এই দুটি ক্লাস সিস্টেমের কার্যক্ষমতা বাড়ায়, কারণ তারা I/O অপারেশনগুলি সহজে একাধিক কাজের সাথে সিঙ্ক্রোনাইজ করে, এবং callback ফাংশনের মাধ্যমে দ্রুত ফলাফল প্রদান করে।
Read more