Java Maven kullanarak hazırlayacağımız projede Eclipse için M2E eklentisini kurmamız gerekiyor. Daha sonra pom.xml dosyasına şunları ekleyeceğiz:

<dependency>
	<groupId>com.rabbitmq</groupId>
	<artifactId>amqp-client</artifactId>
	<version>3.0.4</version>
</dependency>
<dependency>
	<groupId>commons-lang</groupId>
	<artifactId>commons-lang</artifactId>
	<version>2.6</version>
</dependency>

Endpoint.js

package com.marmelatbilisim.rabbitmq;

import java.io.IOException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public abstract class EndPoint {
	protected Channel channel;
	protected Connection connection;
	protected String endPointName;

	public EndPoint(String endpointName) throws IOException {
		this.endPointName = endpointName;

		// Create a connection factory
		ConnectionFactory factory = new ConnectionFactory();

		// hostname of your rabbitmq server
		factory.setHost("localhost");

		// getting a connection
		connection = factory.newConnection();

		// creating a channel
		channel = connection.createChannel();

		// declaring a queue for this channel. If queue does not exist,
		// it will be created on the server.
		channel.queueDeclare(endpointName, false, false, false, null);
	}

	/**
	 * Close channel and connection. Not necessary as it happens implicitly any
	 * way.
	 * 
	 * @throws IOException
	 */
	public void close() throws IOException {
		this.channel.close();
		this.connection.close();
	}
}

Producer.js

package com.marmelatbilisim.rabbitmq;

import java.io.IOException;
import java.io.Serializable;

import org.apache.commons.lang.SerializationUtils;

public class Producer extends EndPoint {
	public Producer(String endPointName) throws IOException {
		super(endPointName);
	}

	public void sendMessage(Serializable object) throws IOException {
		channel.basicPublish("", endPointName, null,
				SerializationUtils.serialize(object));
	}
}

QueueConsumer.js

package com.marmelatbilisim.rabbitmq;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.SerializationUtils;

import com.rabbitmq.client.BasicProperties;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;

public class QueueConsumer extends EndPoint implements Runnable, Consumer {
	public QueueConsumer(String endPointName) throws IOException {
		super(endPointName);
	}

	public void run() {
		try {
			// start consuming messages. Auto acknowledge messages.
			channel.basicConsume(endPointName, true, this);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Called when consumer is registered.
	 */
	public void handleConsumeOk(String consumerTag) {
		System.out.println("Consumer " + consumerTag + " registered");
	}

	/**
	 * Called when new message is available.
	 */
	public void handleDelivery(String consumerTag, Envelope env,
			BasicProperties props, byte[] body) throws IOException {
		Map map = (HashMap) SerializationUtils.deserialize(body);
		System.out.println("Message Number " + map.get("message number")
				+ " received.");

	}

	public void handleCancel(String consumerTag) {
	}

	public void handleCancelOk(String consumerTag) {
	}

	public void handleRecoverOk(String consumerTag) {
	}

	public void handleShutdownSignal(String consumerTag,
			ShutdownSignalException arg1) {
	}

	public void handleDelivery(String arg0, Envelope arg1,
			com.rabbitmq.client.AMQP.BasicProperties arg2, byte[] arg3)
			throws IOException {
		// TODO Auto-generated method stub
		
	}
}

Main.js

package com.marmelatbilisim.rabbitmq;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;

public class Main {
	public Main() throws Exception {

		QueueConsumer consumer = new QueueConsumer("queue");
		Thread consumerThread = new Thread(consumer);
		consumerThread.start();

		Producer producer = new Producer("queue");

		for (int i = 0; i < 100000; i++) {
			HashMap message = new HashMap();
			message.put("message number", i);
			producer.sendMessage(message);
			System.out.println("Message Number " + i + " sent.");
		}
	}

	/**
	 * @param args
	 * @throws SQLException
	 * @throws IOException
	 */
	public static void main(String[] args) throws Exception {
		new Main();
	}
}

Bunları hazırladıktan sonra projeye sağ tıklayıp, Run As dedikten sonra Maven Build seçeneğini seçip, Goal olarak clean install bilgilerini girin. Build işlemi bittikten sonra tekrar projeye sağ tıklayıp, Run As dedikten sonra Java Application seçeneğini seçin.

Comments

  1. Pingback: 3D Black and gray tattoo Khao Lak

  2. Pingback: Spokane Property Listings

  3. Pingback: nembutal kaufen seriös

  4. Pingback: psilocybin Store uk

  5. Pingback: nagaqq

  6. Pingback: นำเข้าสินค้าจากจีน

  7. Pingback: slot

  8. Pingback: บุหรี่นอก

  9. Pingback: รับทำ SEO

  10. Pingback: 789 คาสิโน

  11. Pingback: cz kadet

  12. Pingback: 토렌트 사이트

  13. Pingback: ขายบ้านพัทยา

  14. Pingback: mejaqq

  15. Pingback: https://zsp2.net/

  16. Pingback: RFID Warehouse Management

  17. Pingback: yehyeh.com

  18. Pingback: click here

  19. Pingback: สล็อตเว็บนอก

  20. Pingback: superkaya88

  21. Pingback: ข่าวบอล

  22. Pingback: web

  23. Pingback: führerschein kaufen online

  24. Pingback: slotjili

  25. Pingback: เว็บหวยออนไลน์

  26. Pingback: lsm99

  27. Pingback: ทำกล่องเค้ก

  28. Pingback: ข่าวบอล

  29. Pingback: วิเคราะห์บอลวันนี้

  30. Pingback: 웹툰 다시보기

  31. Pingback: evoplay ค่ายเกมสล็อตแตกง่าย จ่ายหนัก

  32. Pingback: Seal Private Server

  33. Pingback: ประตู wpc

  34. Pingback: spin 238

  35. Pingback: microdose mushrooms for sale usa

  36. Pingback: Cannabis delivery Toronto

  37. Pingback: ทดลองเล่นสล็อต pp

  38. Pingback: site

  39. Pingback: รับจดทะเบียนสมรสกับชาวต่างชาติ

  40. Pingback: adult shop

  41. Pingback: คาสิโนสด Motivation gaming

  42. Pingback: บริการส่ง SMS