//path lib setting
set CLASSPATH=.;lib\amqp-client-5.4.1.jar;lib\*.jar;lib\slf4j-api-1.7.26.jar;
//compile
javac TestSender.java
//run
java TestSender
TestSender.java
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class TestSender
{
private final static String QUEUE_NAME = "rmq";
public static void main ( String [] argv ) throws Exception
{
ConnectionFactory factory = new ConnectionFactory ();
factory.setHost ( "192.168.7.11" );
factory.setPort ( 8890 ); //always give HA port to test if you want to do HA testing. else give queue port
factory.setUsername ( "test" );
factory.setPassword ( "test" );
try (Connection connection = factory.newConnection ();
Channel channel = connection.createChannel ())
{
channel.queueDeclare (QUEUE_NAME, true, false, false, null );
String message = "Hello World111!";
channel.basicPublish ( "", QUEUE_NAME, null, message.getBytes ( "UTF-8" ) );
System.out.println ( " [x] Sent '" + message + "'" );
}
}
}
set CLASSPATH=.;lib\amqp-client-5.4.1.jar;lib\*.jar;lib\slf4j-api-1.7.26.jar;
//compile
javac TestSender.java
//run
java TestSender
TestSender.java
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class TestSender
{
private final static String QUEUE_NAME = "rmq";
public static void main ( String [] argv ) throws Exception
{
ConnectionFactory factory = new ConnectionFactory ();
factory.setHost ( "192.168.7.11" );
factory.setPort ( 8890 ); //always give HA port to test if you want to do HA testing. else give queue port
factory.setUsername ( "test" );
factory.setPassword ( "test" );
try (Connection connection = factory.newConnection ();
Channel channel = connection.createChannel ())
{
channel.queueDeclare (QUEUE_NAME, true, false, false, null );
String message = "Hello World111!";
channel.basicPublish ( "", QUEUE_NAME, null, message.getBytes ( "UTF-8" ) );
System.out.println ( " [x] Sent '" + message + "'" );
}
}
}
TestReceiver.java
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
public class TestReceiver
{
private final static String QUEUE_NAME = "rmq";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost ( "192.168.7.13" );
factory.setPort ( 5672 ); //always receive with your default queue port and not HA port
factory.setUsername ( "test" );
factory.setPassword ( "test" );
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + message + "'");
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
}
}
NOTE: channel.queueDeclare (QUEUE_NAME, true, false, false, null ); //if durable else give false instead of true.
No comments:
Post a Comment