[rabbitmq-discuss] basicpublish Android to Java?

Kamran muhammad.kamrana at gmail.com
Mon Feb 20 13:30:31 GMT 2012


Sorry for my brief message, actually no error is shown. As in java
tutorial one, we get the message ""message" received" I don't receive
anything on the receiver side. "rabbitmqctl list_queues" don't show
any errors either and it shows the creation of queues. By the way I
have tried with different parameters in "exchangedeclare" and
"queuedeclare", but nothing is working for me. Thanks for the help!

The android code is:

public class ActivityHome extends Activity {
	private MessageConsumer mConsumer;
	private TextView mOutput;
    private final static String QUEUE_NAME = "hello";
    private final static String EXCHANGE_NAME = "logs";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //The output TextView we'll use to display messages
        mOutput =  (TextView) findViewById(R.id.output);
       //Create the consumer
        mConsumer = new
MessageConsumer("192.168.2.3","logs","fanout");
        //Connect to broker
        mConsumer.connectToRabbitMQ();
        //register for messages
        mConsumer.setOnReceiveMessageHandler(new
OnReceiveMessageHandler(){
			public void onReceiveMessage(byte[] message) {
				String text = "";
				try {
					text = new String(message, "UTF8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				mOutput.append("\n"+text);
			}
       });
        MessageSender Sender = new
MessageSender("192.168.2.3","logs","fanout");
        Sender.connectToRabbitMQ();
      ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("sethost");
        try{
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Kamran Android E";
        channel.basicPublish(EXCHANGE_NAME, QUEUE_NAME, null,
message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");

        }
        catch (Exception e) {
			// TODO: handle exception
		e.printStackTrace();
        }
       mOutput.append("\nSent");
    }
    @Override
	protected void onResume() {
		super.onPause();
		mConsumer.connectToRabbitMQ();
	}
	@Override
	protected void onPause() {
		super.onPause();
		mConsumer.dispose();
	}
}

This is the receiver code:

public class Recv {
    private final static String QUEUE_NAME = "hello";
    private final static String EXCHANGE_NAME = "logs";
    public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(EXCHANGE_NAME,"fanout", true);
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL
+C");
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, false, consumer);
    while (true) {
      QueueingConsumer.Delivery delivery = consumer.nextDelivery();
      String message = new String(delivery.getBody());
      System.out.println(" [x] Received '" + message + "'");
    }
  }

On Feb 20, 1:31 pm, Emile Joubert <em... at rabbitmq.com> wrote:
> Hi,
>
> On 20/02/12 11:56, Kamran wrote:
>
> >             Yes when I have Java Client in Android. Then a publish via
> > "basicpublish" from Android should go to "basicconsume" in Java file
> > (the receive file in tutorial one). Unfortunately it is not working
> > for me.
>
> When you say this is not working, can you explain what your expectation
> is and how it fails to be met? Did you get any error messages? Does the
> information reported by "rabbitmqctl list_queues" shed any light? Does
> the client receive an exception? In that case how far does it get?
>
> -Emile
> _______________________________________________
> rabbitmq-discuss mailing list
> rabbitmq-disc... at lists.rabbitmq.comhttps://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss


More information about the rabbitmq-discuss mailing list