Bu Sayfayı Paylaş:

Örnek

Active MQ Artemis Producer Örneği

Bu örnekte ActiveMQ Armetis'de bir mesaj üretip kuyruğa ekliyoruz. ActiveMQ Artmesin için aşağıdaki Maven tanımı eklenir:
<dependency>
	<groupId>org.apache.activemq</groupId>
	<artifactId>artemis-jms-client</artifactId>
	<version>2.6.3</version>
</dependency>
Mesaj aşağıdaki uygulama ile test edilebilir:
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQQueue;

public class ArtemisProducerTest {

public static void main(String[] args) {
	Connection connection = null;
	Session session = null;

	try {

		ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://activemqtest.fibiler.com:1234");

		connection = cf.createConnection();

		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

		Queue activeMQQueue=new ActiveMQQueue("queue.FIBILER","TESTQUE"); 
		//ActiveMQQueue activeMQQueue=new ActiveMQQueue("queue.FIBILER::TESTQUE");
		
		MessageProducer messageProducer1 = session.createProducer(activeMQQueue);
		MessageProducer messageProducer2 = session.createProducer(activeMQQueue);
		
		Message msg = session.createTextMessage("Test Mesaji 1");
		
		messageProducer1.send(msg);
		messageProducer2.send(msg);

		System.out.println("Send Mesaj...");		
		
	} catch (JMSException e) {
		e.printStackTrace();
	} finally {
		if (session != null) {
			try {
				session.close();
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}
}
}
Factory olarak ActiveMQConnectionFactory sınıfını kullanıyoruz. Connection ve Session yaratma JMS standarlarına göre yapılır. Kuyruk olarak ActiveMQQueue sınıfı kullanılmalıdır. Bu sınıf bir adres bir de kuyruk adı almaktadır. Ardından iki tane prodecur yaratılır ve mesajlar eklenir.

Örnek

Active MQ Artemis Consumer Örneği

Bu örnekte ActiveMQ Armetis'de kuyruğa eklene mesajları alıyoruz ActiveMQ Artmesin için aşağıdaki Maven tanımı eklenir:
<dependency>
	<groupId>org.apache.activemq</groupId>
	<artifactId>artemis-jms-client</artifactId>
	<version>2.6.3</version>
</dependency>
Eklenen mesajlar aşağıdaki uygulama ile alınabilir:
package com.thy.mercury.activemq.client;

import java.io.IOException;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQQueue;
import org.apache.activemq.artemis.jms.client.ActiveMQSession;

public class ArtemisConsumerTest {

	public static void main(String[] args) {
		Connection connection = null;
		Session session = null;

		try {
			ConnectionFactory cf = 
				new ActiveMQConnectionFactory("tcp://activemqtest.fibiler.com:1234");

			connection = cf.createConnection();

			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

			((ActiveMQSession) session).start();

			ActiveMQQueue activeMQQueue = 
				new ActiveMQQueue("queue.FIBILER::TEST");

			MessageConsumer messageConsumer = 
				session.createConsumer(activeMQQueue);

			messageConsumer.setMessageListener(new MessageListener() {

				@Override
				public void onMessage(Message message) {

					System.out.println(message);

					if (message instanceof TextMessage) {
						try {
							System.out.println(
								((TextMessage) message).getText());
						} catch (JMSException e) {
							e.printStackTrace();
						}
					} else {
						System.out.println(message);
					}

				}
			});

			try {
				System.in.read();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				try {
					session.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

Factory olarak ActiveMQConnectionFactory sınıfını kullanıyoruz. Connection ve Session yaratma JMS standarlarına göre yapılır. Consumer için session başlatılır. Kuyruk olarak ActiveMQQueue sınıfı kullanılmalıdır. Bu sınıf bir adres bir de kuyruk adı almaktadır. Consumer'da bir MessageListener yaratılır ve verilir. En alta eklenen
try {
	System.in.read();
} catch (IOException e) {
	e.printStackTrace();
}
satırları uygulamanın kapanmamasını ve beklemesini sağlamaktadır. Eğer herhangi bir mesaj gelirse ekrana mesaj basılacaktır.

Örnek

Active MQ Artemis QueueBrowser Örneği

Bu örnekte ActiveMQ Armetis'de kuyrukta bulunan mesajları bilgi amaçlı alıyoruz ActiveMQ Artmesin için aşağıdaki Maven tanımı eklenir:
<dependency>
	<groupId>org.apache.activemq</groupId>
	<artifactId>artemis-jms-client</artifactId>
	<version>2.6.3</version>
</dependency>
Eklenen mesajlar aşağıdaki uygulama ile listenelebilir. Bu kod çalıştığında kuyruktaki mesajlar silinmez ve sadece bilgi amaçlı gösterilir:
package com.thy.mercury.activemq.client;

import java.util.Enumeration;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQQueue;

public class ArtemisMonitor {
	public static void main(String[] args) {
		
		Connection connection = null;
		Session session = null;

		try {

			ConnectionFactory cf = 
				new ActiveMQConnectionFactory("tcp://activemqtest.fibiler.com:61616");

			connection = cf.createConnection();

			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	
			Queue activeMQQueue = new ActiveMQQueue("queue.FIBILER::TEST");			
			
			QueueBrowser browser = session.createBrowser(activeMQQueue);
			
			Enumeration e = browser.getEnumeration();
            while (e.hasMoreElements()) {
                TextMessage message = (TextMessage) e.nextElement();
                System.out.println("Browse [" + message.getText() + "]");
            }
            
            browser.close();
			
		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				try {
					session.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
Factory olarak ActiveMQConnectionFactory sınıfını kullanıyoruz. Connection ve Session yaratma JMS standarlarına göre yapılır. Kuyruk olarak ActiveMQQueue sınıfı kullanılmalıdır. Daha sonra QueueBrowser nesnesi yaratılır ve içindeki tüm mesajlar listelenir. Örneğin aşağıdaki gibi bir çıktı olabilir:
Browse [Test 3]
Browse [Test 7]
Browse [Test 11]
Browse [Test 15]
Browse [Test 19]




Bu Sayfayı Paylaş:

İletişim Bilgileri

Takip Et

Her Hakkı Saklıdır. Bu sitede yayınlanan tüm bilgi ve fikirlerin kullanımından fibiler.com sorumlu değildir. Bu sitede üretilmiş , derlenmiş içerikleri, fibiler.com'u kaynak göstermek koşuluyla kendi sitenizde kullanılabilirsiniz. Ancak telif hakkı olan içeriklerin hakları sahiplerine aittir