JAVA-15787 Created new messaging-modules and saas-modules
- Moved jgroups, rabbitmq, spring-amqp, spring-apache-camel, spring-jms to messaging-modules - Moved twilio, twitter4j, strip to saas-modules - Renamed existing saas to jira-rest-integration
This commit is contained in:
+97
@@ -0,0 +1,97 @@
|
||||
package com.baeldung.benchmark;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.LongSummaryStatistics;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.baeldung.benchmark.Worker.WorkerResult;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.Connection;
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
|
||||
public class ConnectionPerChannelPublisher implements Callable<Long> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ConnectionPerChannelPublisher.class);
|
||||
private final ConnectionFactory factory;
|
||||
private final int workerCount;
|
||||
private final int iterations;
|
||||
private final int payloadSize;
|
||||
|
||||
ConnectionPerChannelPublisher(ConnectionFactory factory, int workerCount, int iterations, int payloadSize) {
|
||||
this.factory = factory;
|
||||
this.workerCount = workerCount;
|
||||
this.iterations = iterations;
|
||||
this.payloadSize = payloadSize;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
if (args.length != 4) {
|
||||
System.err.println("Usage: java " + ConnectionPerChannelPublisher.class.getName() + " <host> <#channels> <#messages> <payloadSize>");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost(args[0]);
|
||||
|
||||
int workerCount = Integer.parseInt(args[1]);
|
||||
int iterations = Integer.parseInt(args[2]);
|
||||
int payloadSize = Integer.parseInt(args[3]);
|
||||
|
||||
// run the benchmark 10x and get the average throughput
|
||||
LongSummaryStatistics summary = IntStream.range(0, 9)
|
||||
.mapToObj(idx -> new ConnectionPerChannelPublisher(factory, workerCount, iterations, payloadSize))
|
||||
.map(p -> p.call())
|
||||
.collect(Collectors.summarizingLong((l) -> l));
|
||||
|
||||
log.info("[I66] workers={}, throughput={}", workerCount, (int)Math.floor(summary.getAverage()));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long call() {
|
||||
try {
|
||||
List<Worker> workers = new ArrayList<>();
|
||||
CountDownLatch counter = new CountDownLatch(workerCount);
|
||||
|
||||
for (int i = 0; i < workerCount; i++) {
|
||||
Connection conn = factory.newConnection();
|
||||
workers.add(new Worker("queue_" + i, conn, iterations, counter, payloadSize));
|
||||
}
|
||||
|
||||
ExecutorService executor = new ThreadPoolExecutor(workerCount, workerCount, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(workerCount, true));
|
||||
long start = System.currentTimeMillis();
|
||||
log.info("[I61] Starting {} workers...", workers.size());
|
||||
executor.invokeAll(workers);
|
||||
if (counter.await(5, TimeUnit.MINUTES)) {
|
||||
long elapsed = System.currentTimeMillis() - start;
|
||||
log.info("[I59] Tasks completed: #workers={}, #iterations={}, elapsed={}ms, stats={}", workerCount, iterations, elapsed);
|
||||
return throughput(workerCount, iterations, elapsed);
|
||||
} else {
|
||||
throw new RuntimeException("[E61] Timeout waiting workers to complete");
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static long throughput(int workerCount, int iterations, long elapsed) {
|
||||
return (iterations * workerCount * 1000) / elapsed;
|
||||
}
|
||||
|
||||
}
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
package com.baeldung.benchmark;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.LongSummaryStatistics;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.Connection;
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
|
||||
public class SharedConnectionPublisher {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SharedConnectionPublisher.class);
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
|
||||
if ( args.length != 6) {
|
||||
System.err.println("Usage: java " + SharedConnectionPublisher.class.getName() + " <host> <#channels> <#messages> <payloadSize> <#channels/connection> <extra work time(ms)>");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost(args[0]);
|
||||
|
||||
List<Worker> workers = new ArrayList<>();
|
||||
|
||||
int workerCount = Integer.parseInt(args[1]);
|
||||
int iterations = Integer.parseInt(args[2]);
|
||||
int payloadSize = Integer.parseInt(args[3]);
|
||||
int channelsPerConnection = Integer.parseInt(args[4]);
|
||||
long extraWork = Long.parseLong(args[5]);
|
||||
|
||||
log.info("[I35] Creating {} worker{}...", workerCount, (workerCount > 1)?"s":"");
|
||||
|
||||
CountDownLatch counter = new CountDownLatch(workerCount);
|
||||
|
||||
int connCount = (workerCount + channelsPerConnection-1)/channelsPerConnection;
|
||||
List<Connection> connections = new ArrayList<>(connCount);
|
||||
for( int i =0 ; i< connCount; i++) {
|
||||
log.info("[I59] Creating connection#{}", i);
|
||||
connections.add(factory.newConnection());
|
||||
}
|
||||
|
||||
for( int i = 0 ; i < workerCount ; i++ ) {
|
||||
workers.add(new Worker("queue_" + i, connections.get(i % connCount), iterations, counter,payloadSize,extraWork));
|
||||
}
|
||||
|
||||
ExecutorService executor = new ThreadPoolExecutor(workerCount, workerCount, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(workerCount, true));
|
||||
long start = System.currentTimeMillis();
|
||||
log.info("[I61] Starting workers...");
|
||||
List<Future<WorkerResult>> results = executor.invokeAll(workers);
|
||||
|
||||
log.info("[I55] Waiting workers to complete...");
|
||||
if( counter.await(5, TimeUnit.MINUTES) ) {
|
||||
long elapsed = System.currentTimeMillis() - start - (workerCount*iterations*extraWork);
|
||||
log.info("[I59] Tasks completed: #workers={}, #iterations={}, elapsed={}ms",
|
||||
workerCount,
|
||||
iterations,
|
||||
elapsed);
|
||||
|
||||
LongSummaryStatistics summary = results.stream()
|
||||
.map(f -> safeGet(f))
|
||||
.map(r -> r.elapsed)
|
||||
.collect(Collectors.summarizingLong((l) -> l));
|
||||
|
||||
log.info("[I74] stats={}", summary);
|
||||
log.info("[I79] result: workers={}, throughput={}",workerCount,throughput(workerCount,iterations,elapsed));
|
||||
|
||||
}
|
||||
else {
|
||||
log.error("[E61] Timeout waiting workers to complete");
|
||||
}
|
||||
|
||||
}
|
||||
catch(Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static long throughput(int workerCount, int iterations, long elapsed) {
|
||||
return (iterations*workerCount*1000)/elapsed;
|
||||
}
|
||||
|
||||
|
||||
private static <T> T safeGet(Future<T> f) {
|
||||
try {
|
||||
return f.get();
|
||||
}
|
||||
catch(Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static class WorkerResult {
|
||||
public final long elapsed;
|
||||
WorkerResult(long elapsed) {
|
||||
this.elapsed = elapsed;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class Worker implements Callable<WorkerResult> {
|
||||
|
||||
private final Connection conn;
|
||||
private final Channel channel;
|
||||
private int iterations;
|
||||
private final CountDownLatch counter;
|
||||
private final String queue;
|
||||
private final byte[] payload;
|
||||
private long extraWork;
|
||||
|
||||
Worker(String queue, Connection conn, int iterations, CountDownLatch counter,int payloadSize,long extraWork) throws IOException {
|
||||
this.conn = conn;
|
||||
this.iterations = iterations;
|
||||
this.counter = counter;
|
||||
this.queue = queue;
|
||||
this.extraWork = extraWork;
|
||||
|
||||
channel = conn.createChannel();
|
||||
channel.queueDeclare(queue, false, false, true, null);
|
||||
|
||||
this.payload = new byte[payloadSize];
|
||||
new Random().nextBytes(payload);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public WorkerResult call() throws Exception {
|
||||
|
||||
try {
|
||||
long start = System.currentTimeMillis();
|
||||
for ( int i = 0 ; i < iterations ; i++ ) {
|
||||
channel.basicPublish("", queue, null,payload);
|
||||
Thread.sleep(extraWork);
|
||||
}
|
||||
|
||||
long elapsed = System.currentTimeMillis() - start - (extraWork*iterations);
|
||||
channel.queueDelete(queue);
|
||||
return new WorkerResult(elapsed);
|
||||
}
|
||||
finally {
|
||||
counter.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
package com.baeldung.benchmark;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.LongSummaryStatistics;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.baeldung.benchmark.Worker.WorkerResult;
|
||||
import com.rabbitmq.client.Connection;
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
|
||||
public class SingleConnectionPublisher implements Callable<Long> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SingleConnectionPublisher.class);
|
||||
|
||||
private final ConnectionFactory factory;
|
||||
private final int workerCount;
|
||||
private final int iterations;
|
||||
private final int payloadSize;
|
||||
|
||||
SingleConnectionPublisher(ConnectionFactory factory, int workerCount, int iterations, int payloadSize) {
|
||||
this.factory = factory;
|
||||
this.workerCount = workerCount;
|
||||
this.iterations = iterations;
|
||||
this.payloadSize = payloadSize;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
if ( args.length != 4) {
|
||||
System.err.println("Usage: java " + SingleConnectionPublisher.class.getName() + " <host> <#channels> <#messages> <payloadSize>");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost(args[0]);
|
||||
|
||||
int workerCount = Integer.parseInt(args[1]);
|
||||
int iterations = Integer.parseInt(args[2]);
|
||||
int payloadSize = Integer.parseInt(args[3]);
|
||||
|
||||
LongSummaryStatistics summary = IntStream.range(0, 9)
|
||||
.mapToObj(idx -> new SingleConnectionPublisher(factory, workerCount, iterations, payloadSize))
|
||||
.map(p -> p.call())
|
||||
.collect(Collectors.summarizingLong((l) -> l));
|
||||
|
||||
log.info("[I66] workers={}, throughput={}", workerCount, (int)Math.floor(summary.getAverage()));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long call() {
|
||||
|
||||
try {
|
||||
|
||||
Connection connection = factory.newConnection();
|
||||
CountDownLatch counter = new CountDownLatch(workerCount);
|
||||
List<Worker> workers = new ArrayList<>();
|
||||
|
||||
for( int i = 0 ; i < workerCount ; i++ ) {
|
||||
workers.add(new Worker("queue_" + i, connection, iterations, counter,payloadSize));
|
||||
}
|
||||
|
||||
ExecutorService executor = new ThreadPoolExecutor(workerCount, workerCount, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(workerCount, true));
|
||||
long start = System.currentTimeMillis();
|
||||
log.info("[I61] Starting {} workers...", workers.size());
|
||||
List<Future<WorkerResult>> results = executor.invokeAll(workers);
|
||||
|
||||
if( counter.await(5, TimeUnit.MINUTES) ) {
|
||||
long elapsed = System.currentTimeMillis() - start;
|
||||
|
||||
LongSummaryStatistics summary = results.stream()
|
||||
.map(f -> safeGet(f))
|
||||
.map(r -> r.elapsed)
|
||||
.collect(Collectors.summarizingLong((l) -> l));
|
||||
|
||||
log.info("[I59] Tasks completed: #workers={}, #iterations={}, elapsed={}ms, stats={}",
|
||||
workerCount,
|
||||
iterations,
|
||||
elapsed, summary);
|
||||
|
||||
return throughput(workerCount,iterations,elapsed);
|
||||
}
|
||||
else {
|
||||
throw new RuntimeException("[E61] Timeout waiting workers to complete");
|
||||
}
|
||||
}
|
||||
catch(Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> T safeGet(Future<T> f) {
|
||||
try {
|
||||
return f.get();
|
||||
}
|
||||
catch(Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static long throughput(int workerCount, int iterations, long elapsed) {
|
||||
return (iterations*workerCount*1000)/elapsed;
|
||||
}
|
||||
}
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
package com.baeldung.benchmark;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.LongSummaryStatistics;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.Connection;
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
|
||||
public class SingleConnectionPublisherNio {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SingleConnectionPublisherNio.class);
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
|
||||
if ( args.length != 4) {
|
||||
System.err.println("Usage: java " + SingleConnectionPublisherNio.class.getName() + " <host> <#channels> <#messages> <payloadSize>");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost(args[0]);
|
||||
factory.useNio();
|
||||
Connection connection = factory.newConnection();
|
||||
|
||||
List<Worker> workers = new ArrayList<>();
|
||||
|
||||
int workerCount = Integer.parseInt(args[1]);
|
||||
int iterations = Integer.parseInt(args[2]);
|
||||
int payloadSize = Integer.parseInt(args[3]);
|
||||
|
||||
log.info("[I35] Creating {} worker{}...", workerCount, (workerCount > 1)?"s":"");
|
||||
|
||||
CountDownLatch counter = new CountDownLatch(workerCount);
|
||||
|
||||
for( int i = 0 ; i < workerCount ; i++ ) {
|
||||
workers.add(new Worker("queue_" + i, connection, iterations, counter,payloadSize));
|
||||
}
|
||||
|
||||
ExecutorService executor = new ThreadPoolExecutor(workerCount, workerCount, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(workerCount, true));
|
||||
long start = System.currentTimeMillis();
|
||||
log.info("[I61] Starting workers...");
|
||||
List<Future<WorkerResult>> results = executor.invokeAll(workers);
|
||||
|
||||
log.info("[I55] Waiting workers to complete...");
|
||||
if( counter.await(5, TimeUnit.MINUTES) ) {
|
||||
long elapsed = System.currentTimeMillis() - start;
|
||||
log.info("[I59] Tasks completed: #workers={}, #iterations={}, elapsed={}ms",
|
||||
workerCount,
|
||||
iterations,
|
||||
elapsed);
|
||||
|
||||
LongSummaryStatistics summary = results.stream()
|
||||
.map(f -> safeGet(f))
|
||||
.map(r -> r.elapsed)
|
||||
.collect(Collectors.summarizingLong((l) -> l));
|
||||
|
||||
log.info("[I74] stats={}", summary);
|
||||
|
||||
}
|
||||
else {
|
||||
log.error("[E61] Timeout waiting workers to complete");
|
||||
}
|
||||
|
||||
}
|
||||
catch(Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static <T> T safeGet(Future<T> f) {
|
||||
try {
|
||||
return f.get();
|
||||
}
|
||||
catch(Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static class WorkerResult {
|
||||
public final long elapsed;
|
||||
WorkerResult(long elapsed) {
|
||||
this.elapsed = elapsed;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class Worker implements Callable<WorkerResult> {
|
||||
|
||||
private final Connection conn;
|
||||
private final Channel channel;
|
||||
private int iterations;
|
||||
private final CountDownLatch counter;
|
||||
private final String queue;
|
||||
private final byte[] payload;
|
||||
|
||||
Worker(String queue, Connection conn, int iterations, CountDownLatch counter,int payloadSize) throws IOException {
|
||||
this.conn = conn;
|
||||
this.iterations = iterations;
|
||||
this.counter = counter;
|
||||
this.queue = queue;
|
||||
|
||||
channel = conn.createChannel();
|
||||
channel.queueDeclare(queue, false, false, true, null);
|
||||
|
||||
this.payload = new byte[payloadSize];
|
||||
new Random().nextBytes(payload);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public WorkerResult call() throws Exception {
|
||||
|
||||
try {
|
||||
long start = System.currentTimeMillis();
|
||||
for ( int i = 0 ; i < iterations ; i++ ) {
|
||||
channel.basicPublish("", queue, null,payload);
|
||||
}
|
||||
|
||||
long elapsed = System.currentTimeMillis() - start;
|
||||
channel.queueDelete(queue);
|
||||
return new WorkerResult(elapsed);
|
||||
}
|
||||
finally {
|
||||
counter.countDown();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.benchmark;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.Connection;
|
||||
|
||||
public class Worker implements Callable<Worker.WorkerResult> {
|
||||
|
||||
private final Channel channel;
|
||||
private int iterations;
|
||||
private final CountDownLatch counter;
|
||||
private final String queue;
|
||||
private final byte[] payload;
|
||||
|
||||
Worker(String queue, Connection conn, int iterations, CountDownLatch counter, int payloadSize) throws IOException {
|
||||
this.iterations = iterations;
|
||||
this.counter = counter;
|
||||
this.queue = queue;
|
||||
|
||||
channel = conn.createChannel();
|
||||
channel.queueDelete(queue);
|
||||
channel.queueDeclare(queue, false, false, true, null);
|
||||
|
||||
this.payload = new byte[payloadSize];
|
||||
new Random().nextBytes(payload);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public WorkerResult call() throws Exception {
|
||||
|
||||
try {
|
||||
long start = System.currentTimeMillis();
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
channel.basicPublish("", queue, null, payload);
|
||||
}
|
||||
|
||||
long elapsed = System.currentTimeMillis() - start;
|
||||
channel.queueDelete(queue);
|
||||
return new WorkerResult(elapsed);
|
||||
} finally {
|
||||
counter.countDown();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class WorkerResult {
|
||||
public final long elapsed;
|
||||
|
||||
WorkerResult(long elapsed) {
|
||||
this.elapsed = elapsed;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.consumer;
|
||||
|
||||
import com.rabbitmq.client.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public class Receiver {
|
||||
|
||||
private static final String QUEUE_NAME = "products_queue";
|
||||
|
||||
public static void main (String[] args) throws IOException, TimeoutException {
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost("localhost");
|
||||
Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
|
||||
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
|
||||
|
||||
DefaultConsumer consumer = new DefaultConsumer(channel) {
|
||||
@Override
|
||||
public void handleDelivery(String consumerTag,
|
||||
Envelope envelope, AMQP.BasicProperties properties,
|
||||
byte[] body) throws IOException {
|
||||
String message = new String(body, "UTF-8");
|
||||
System.out.println(" [x] Received '" + message + "'");
|
||||
}
|
||||
};
|
||||
channel.basicConsume(QUEUE_NAME, true, consumer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.producer;
|
||||
|
||||
import com.rabbitmq.client.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public class Publisher {
|
||||
|
||||
private final static String QUEUE_NAME = "products_queue";
|
||||
|
||||
public static void main(String[]args) throws IOException, TimeoutException {
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost("localhost");
|
||||
Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
|
||||
String message = "product details";
|
||||
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
|
||||
|
||||
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
|
||||
System.out.println(" [x] Sent '" + message + "'");
|
||||
|
||||
channel.close();
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.pubsubmq.client;
|
||||
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
|
||||
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class ClientApplication {
|
||||
private static final String MESSAGE_QUEUE = "pizza-message-queue";
|
||||
|
||||
@Bean
|
||||
public Queue queue() {
|
||||
return new Queue(MESSAGE_QUEUE);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
|
||||
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
|
||||
container.setConnectionFactory(connectionFactory);
|
||||
container.setQueueNames(MESSAGE_QUEUE);
|
||||
container.setMessageListener(listenerAdapter);
|
||||
return container;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Consumer consumer() {
|
||||
return new Consumer();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MessageListenerAdapter listenerAdapter(Consumer consumer) {
|
||||
return new MessageListenerAdapter(consumer, "receiveOrder");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ClientApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.pubsubmq.client;
|
||||
|
||||
public class Consumer {
|
||||
public void receiveOrder(String message) {
|
||||
System.out.printf("Order received: %s%n", message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.pubsubmq.server;
|
||||
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
public class Publisher {
|
||||
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
private String queue;
|
||||
private String topic;
|
||||
|
||||
public Publisher(RabbitTemplate rabbitTemplate, String queue, String topic) {
|
||||
this.rabbitTemplate = rabbitTemplate;
|
||||
this.queue = queue;
|
||||
this.topic = topic;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void postMessages() {
|
||||
rabbitTemplate.convertAndSend(queue, "1 Pepperoni");
|
||||
rabbitTemplate.convertAndSend(queue, "3 Margarita");
|
||||
rabbitTemplate.convertAndSend(queue, "1 Ham and Pineapple (yuck)");
|
||||
|
||||
rabbitTemplate.convertAndSend(topic, "notification", "New Deal on T-Shirts: 95% off!");
|
||||
rabbitTemplate.convertAndSend(topic, "notification", "2 for 1 on all Jeans!");
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.pubsubmq.server;
|
||||
|
||||
import org.springframework.amqp.core.Binding;
|
||||
import org.springframework.amqp.core.BindingBuilder;
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.amqp.core.TopicExchange;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ServerApplication {
|
||||
private static final String MESSAGE_QUEUE = "pizza-message-queue";
|
||||
private static final String PUB_SUB_TOPIC = "notification-topic";
|
||||
private static final String PUB_SUB_EMAIL_QUEUE = "email-queue";
|
||||
private static final String PUB_SUB_TEXT_QUEUE = "text-queue";
|
||||
|
||||
@Bean
|
||||
public Queue queue() {
|
||||
return new Queue(MESSAGE_QUEUE);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Queue emailQueue() {
|
||||
return new Queue(PUB_SUB_EMAIL_QUEUE);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Queue textQueue() {
|
||||
return new Queue(PUB_SUB_TEXT_QUEUE);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TopicExchange exchange() {
|
||||
return new TopicExchange(PUB_SUB_TOPIC);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Binding emailBinding(Queue emailQueue, TopicExchange exchange) {
|
||||
return BindingBuilder.bind(emailQueue).to(exchange).with("notification");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Binding textBinding(Queue textQueue, TopicExchange exchange) {
|
||||
return BindingBuilder.bind(textQueue).to(exchange).with("notification");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Publisher publisher(RabbitTemplate rabbitTemplate) {
|
||||
return new Publisher(rabbitTemplate, MESSAGE_QUEUE, PUB_SUB_TOPIC);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ServerApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.setup;
|
||||
|
||||
import com.rabbitmq.client.BuiltinExchangeType;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.Connection;
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public class Setup {
|
||||
|
||||
private static final String ORDERS_QUEUE_NAME = "orders-queue";
|
||||
private static final String ORDERS_EXCHANGE_NAME = "orders-direct-exchange";
|
||||
private static final String ORDERS_ALTERNATE_EXCHANGE_NAME = "orders-alternate-exchange";
|
||||
private static final String ORDERS_ROUTING_KEY = "orders-routing-key";
|
||||
|
||||
public static void main(String[] args) throws IOException, TimeoutException {
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost("localhost");
|
||||
Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
|
||||
Map<String, Object> exchangeArguments = new HashMap<>();
|
||||
exchangeArguments.put("alternate-exchange", ORDERS_ALTERNATE_EXCHANGE_NAME);
|
||||
channel.exchangeDeclare(ORDERS_EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true, false, exchangeArguments);
|
||||
|
||||
Map<String, Object> queueArguments = new HashMap<>();
|
||||
queueArguments.put("x-message-ttl", 60000);
|
||||
queueArguments.put("x-max-priority", 10);
|
||||
channel.queueDeclare(ORDERS_QUEUE_NAME, true, false, false, queueArguments);
|
||||
|
||||
channel.queueBind(ORDERS_QUEUE_NAME, ORDERS_EXCHANGE_NAME, ORDERS_ROUTING_KEY);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,2 @@
|
||||
# Memory configuration for Rabbit
|
||||
vm_memory_high_watermark.relative = 0.8
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.benchmark;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ConnectionPerChannelPublisherLiveTest {
|
||||
|
||||
@Test
|
||||
void whenConnectionPerChannel_thenRunBenchmark() throws Exception {
|
||||
// host, workerCount, iterations, payloadSize
|
||||
Arrays.asList(1,5,10,20,50,100,150).stream()
|
||||
.forEach(workers -> {
|
||||
ConnectionPerChannelPublisher.main(new String[]{"192.168.99.100", Integer.toString(workers), "1000", "4096"});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.benchmark;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SingleConnectionPublisherLiveTest {
|
||||
|
||||
@Test
|
||||
void whenSingleChannel_thenRunBenchmark() throws Exception {
|
||||
// host, workerCount, iterations, payloadSize
|
||||
Arrays.asList(1,5,10,20,50,100,150).stream()
|
||||
.forEach(workers -> {
|
||||
SingleConnectionPublisher.main(new String[]{"192.168.99.100", Integer.toString(workers), "1000", "4096"});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user