Spring Boot এবং Apache ActiveMQ এর ইন্টিগ্রেশন ব্যবহার করে আপনি সহজেই মেসেজিং সিস্টেম তৈরি করতে পারেন, যেখানে Spring Boot অ্যাপ্লিকেশন থেকে ActiveMQ ব্রোকারের মাধ্যমে মেসেজ পাঠানো এবং গ্রহণ করা যাবে। অ্যাপাচি অ্যাকটিভএমকিউ হলো একটি ওপেন সোর্স মেসেজ ব্রোকার যা JMS (Java Message Service) প্রটোকল সাপোর্ট করে এবং Spring Boot এর সাথে সহজে ইন্টিগ্রেট করা যায়।
এই টিউটোরিয়ালে, আমরা দেখব কিভাবে Spring Boot অ্যাপ্লিকেশনে ActiveMQ সেটআপ করা যায় এবং মেসেজ প্রোডিউসার এবং কনজিউমার তৈরি করা যায়।
প্রথমে, আপনার Spring Boot অ্যাপ্লিকেশন তৈরির জন্য প্রয়োজনীয় ডিপেনডেন্সি এবং সেটআপ করতে হবে। আপনি Spring Initializr ব্যবহার করে একটি নতুন Spring Boot প্রজেক্ট তৈরি করতে পারেন। প্রজেক্টের জন্য ডিপেনডেন্সি নির্বাচন করুন:
Spring Boot অ্যাপ্লিকেশনটির pom.xml ফাইলে প্রয়োজনীয় ডিপেনডেন্সি যোগ করতে হবে। এটি আপনাকে ActiveMQ এবং Spring JMS ব্যবহার করতে সাহায্য করবে।
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter JMS -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!-- Spring Boot Starter for JMS (Apache ActiveMQ) -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
আপনার Spring Boot অ্যাপ্লিকেশনকে ActiveMQ ব্রোকারের সাথে সংযোগ স্থাপন করার জন্য application.properties
ফাইলে কনফিগারেশন যোগ করতে হবে।
# ActiveMQ কনফিগারেশন
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=true
এখানে:
Spring Boot অ্যাপ্লিকেশনে ActiveMQ ইন্টিগ্রেট করার জন্য, আপনি একটি কনফিগারেশন ক্লাস তৈরি করতে পারেন যা ActiveMQ এর সাথে Spring JMS-এর সংযোগ স্থাপন করবে।
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
@Configuration
public class ActiveMQConfig {
@Bean
public ActiveMQConnectionFactory connectionFactory() {
return new ActiveMQConnectionFactory("tcp://localhost:61616");
}
@Bean
public CachingConnectionFactory cachingConnectionFactory(ActiveMQConnectionFactory connectionFactory) {
return new CachingConnectionFactory(connectionFactory);
}
@Bean
public JmsTemplate jmsTemplate(CachingConnectionFactory cachingConnectionFactory) {
return new JmsTemplate(cachingConnectionFactory);
}
}
এখানে:
ActiveMQ এর মাধ্যমে মেসেজ পাঠানোর জন্য একটি প্রোডিউসার ক্লাস তৈরি করতে হবে। এটি JmsTemplate
ব্যবহার করে মেসেজ ActiveMQ কিউতে পাঠাবে।
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
@Component
public class MessageProducer {
@Autowired
private JmsTemplate jmsTemplate;
public void sendMessage(String message) {
jmsTemplate.convertAndSend("testQueue", message);
}
}
এখানে:
এখন, আপনাকে একটি কনজিউমার তৈরি করতে হবে, যা ActiveMQ কিউ থেকে মেসেজ গ্রহণ করবে।
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class MessageConsumer {
@JmsListener(destination = "testQueue")
public void receiveMessage(String message) {
System.out.println("Received message: " + message);
}
}
এখানে:
Spring Boot অ্যাপ্লিকেশন রান করার জন্য একটি মূল ক্লাস তৈরি করুন।
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Autowired;
@SpringBootApplication
public class ActiveMQApplication implements CommandLineRunner {
@Autowired
private MessageProducer messageProducer;
public static void main(String[] args) {
SpringApplication.run(ActiveMQApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
messageProducer.sendMessage("Hello, ActiveMQ from Spring Boot!");
}
}
এখানে:
এখন, আপনি Spring Boot অ্যাপ্লিকেশন চালাতে পারবেন। যদি আপনার অ্যাকটিভএমকিউ ব্রোকার রান না থাকে, তাহলে প্রথমে সেটি চালু করুন (যেমন: activemq start
কমান্ড দিয়ে)। এরপর Spring Boot অ্যাপ্লিকেশনটি চালু করুন:
mvn spring-boot:run
এটি মেসেজ প্রোডিউসারকে testQueue
কিউতে একটি মেসেজ পাঠাতে সক্ষম হবে। মেসেজ কনজিউমার তা গ্রহণ করবে এবং কনসোল এ দেখাবে।
Spring Boot এবং Apache ActiveMQ এর ইন্টিগ্রেশন একটি শক্তিশালী এবং স্কেলেবল মেসেজিং সিস্টেম তৈরি করতে সাহায্য করে। Spring Boot অ্যাপ্লিকেশন থেকে ActiveMQ ব্রোকারের মাধ্যমে মেসেজ প্রেরণ এবং গ্রহণ করা সহজে করা যায়। এখানে ব্যবহৃত JmsTemplate এবং @JmsListener অ্যাপাচি অ্যাকটিভএমকিউ-এর সাথে সংযুক্ত হয়ে কার্যকরীভাবে মেসেজ প্রোসেসিং করতে সক্ষম।
common.read_more