Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ Sending objects

Hello I'm new to RabbitMQ, I'm trying to send an object between 2 applications, but it's not working I then found out that I need to serialize and deserialize the objects but it's still not working. When I'm sending a String there is no problem, but with objects it's not working suddenly there is no connection between the application, I'm not sure what I'm doing wrong.

Here is the Sender.java :

import org.apache.commons.lang3.SerializationUtils;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class App {

private final static String QUEUE_NAME = "12345";

public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();

    factory.setHost("somehost"); 
    factory.setUsername("guest"); 
    // factory.setPassword( "password" ); 
    //factory.setPort( 12345 ); 

    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    Car car = new Car(4, 4, "Mercedes");

    byte[] data = SerializationUtils.serialize(car);

    channel.basicPublish("", QUEUE_NAME, null, data);
    System.out.println(" [x] Sent '" + data + "'");

    channel.close();
    connection.close();
   }
}

Here is Receiver.java :

import org.apache.commons.lang3.SerializationUtils;
import com.rabbitmq.client.*;
import java.io.IOException;

public class Recipient {

     private final static String QUEUE_NAME = "12345";

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("Somehost");
    factory.setUsername("guest"); 
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    Consumer 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");
            //byte [] data = new byte[];

            Object object =    SerializationUtils.deserialize(message.getBytes());

            System.out.println(" [x] Received '" + object + "'");
         }
      };
    channel.basicConsume(QUEUE_NAME, true, consumer);
     }
    }
like image 820
Peter Barnes Avatar asked Nov 08 '22 15:11

Peter Barnes


1 Answers

@Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
            throws IOException {
        //byte [] data = new byte[];

        Object object =    SerializationUtils.deserialize(body);

        System.out.println(" [x] Received '" + object + "'");
     }
  };

Just deserialize directly from the body variable

like image 114
Ludwik Bukowski Avatar answered Nov 14 '22 21:11

Ludwik Bukowski