FreeKB - RabbitMQ Connect to RabbitMQ in Java
RabbitMQ - Connect to RabbitMQ in Java

Create a Dynamic Web Project in Eclipse. Let's say the name of the project is rabbitmq. Download amqp-client-<version>.jar and javax.jms-api-<version>.jar and add the JAR files to your build path in Eclipse.

 

In the left panel of Eclipse, at the name of your project (rabbitmq in this example) > Java Resources > src, create a new package. For the sake of this tutorial, let's name the package com.rabbitmq.demo.

 

In the package, create a class called connect.

 

In connect.java, add the following markup. In this example, notice the setUri directive will attempt to connect to server1.example.com on port 5671 to the "foo" virtual host as john.doe.

package com.rabbitmq.demo;

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

public class connect {

  public void amqConnect() {	
    ConnectionFactory cf = new ConnectionFactory();

    // use amqp if the server is not using SSL (e.g. http)
    // use amqps if the server is using SSL (e.g. https)
    cf.setUri("amqps://john.doe:itsasecret@server1.example.com:5671/foo");

    Connection conn = null;
    try {
      System.out.println("trying to connect");
      conn = cf.newConnection();
    } 
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {    
      if (conn != null) {    
        System.out.println("connection successfully created");
        try {
          conn.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
          System.out.println("connection successfully closed");
        }
      }
      else {
        System.out.println("connection failed");   		  
      }
    }            
  }
}

 

Or instead of using setUri, the following can be used.

cf.setUsername("john.doe");
cf.setPassword("itsasecret");
cf.setVirtualHost("foo");
cf.setHost("rabbit1.example.com");
cf.setPort(5671);

 

In the left panel of Eclipse, right click on the name of your project (rabbitmq in this example) and select New JSP File. Select the WebContent folder, give the file a name such as index.jsp, and select Finish.

 

Add the following to line 1 of the JSP file. This is the combination of the package (com.rabbitmq.main) and the class (connect.java).

<%@page import="com.rabbitmq.main.connect"%>

 

Add the following inside the <body> tags.

  • In this example, "connect" must be used because the name of the class in connect.java is connect.
  • In this example, "amqConnect" must be cause because the name of the method in connect.java is amqConnect.
<body>
<%
  connect jms = new connect();
  jms.amqConnect();
%>
</body>

 

Select the green play button in Eclipse, and hopefully something like this is returned.

 

Likewise, the main RabbitMQ log file should have something like this.

accepting AMQP connection <0.7993.1338> (10.17.115.16:54343 -> 10.122.15.2:5672)

connection <0.7993.1338> (10.17.115.16:54343 -> 10.122.15.2:5672): user 'john.doe' authenticated and granted access to vhost '/'

closing AMQP connection <0.21250.1339> (10.17.115.16:54343 -> 10.122.15.2:5672, vhost: '/', user: 'john.doe')

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 9ecb0 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |