$ ./rabbitmq-server RabbitMQ 3.5.4. Copyright (C) 2007-2015 Pivotal Software, Inc. ## ## Licensed under the MPL. See http://www.rabbitmq.com/ ## ## ########## Logs: ./../var/log/rabbitmq/rabbit@kyle-no-MacBook.log ###### ## ./../var/log/rabbitmq/rabbit@kyle-no-MacBook-sasl.log ########## Starting broker... completed with 0 plugins.
Using RabbitMQ with Java
TweetPosted on Wednesday Aug 05, 2015 at 05:04PM in Technology
RabbitMQ is an open source message broker software made with Erlang. RabbitMQ is sponsored by Pivotal Software, Inc. in this entry, we’ll see how to launch it, publish or receive simple messages with Java client on OS X.
Installation
I’m using OS X 10.9.5. download rabbitmq-server-mac-standalone-3.5.4.tar.gz
for Macs then extract it on somewhere.
Start the server
The server can be launched via execute sbin/rabbitmq-server
as follows.
To shutdown, hit Ctrl+C
then press a
.
Next, we’ll look how to interact with the server from Java client.
Dependencies
RabbitMQ supplies the client library for Java in Maven Central. just put following dependency to your pom.xml
.
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.5.4</version> </dependency>
Java examples in official tutorial
There is a complete Hello World example of a publisher in https://www.rabbitmq.com/tutorials/tutorial-one-java.html . just copy and paste it to your workspace and execute it after launch the server.
Complete a consumer example is here: https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/java/Recv.java . just execute it after execution of publisher is done.
The whole project can be obtained from my GitHub repository, nothing interesting here though.
Remarks
It’s very easy to use, I haven’t experienced any pitfall at all.
I wanted to interact with the server using JMS, but the JMS client needs a commercial license to download and unfortunately I don’t have one.
References
Tags: rabbitmq
Lean example of Tomcat 8 + Guice 4 + Jersey 2.19
TweetPosted on Wednesday Aug 05, 2015 at 02:36PM in Technology
Jersey is the RI of JAX-RS. in this entry, I introduce you how to use Jersey 2.19 with Guice 4 on Tomcat 8. it looks like there are some issues exist as follows but thanks to https://github.com/Squarespace/jersey2-guice , I’ve succeeded to use them anyway.
The entire project which based on Maven can be obtained from My GitHub repository.
Dependencies
<dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey</groupId> <artifactId>jersey-bom</artifactId> <version>2.19</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> </dependency> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>4.0</version> </dependency> <dependency> <groupId>com.google.inject.extensions</groupId> <artifactId>guice-servlet</artifactId> <version>4.0</version> </dependency> <dependency> <groupId>com.squarespace.jersey2-guice</groupId> <artifactId>jersey2-guice</artifactId> <version>0.10</version> </dependency> </dependencies>
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>Jersey Web Application</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>guice.tomcat.jersey</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey Web Application</servlet-name> <url-pattern>/webapi/*</url-pattern> </servlet-mapping> <listener> <listener-class>guice.tomcat.MyJerseyGuiceServletContextListener</listener-class> </listener> <filter> <filter-name>guiceFilter</filter-name> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class> </filter> <filter-mapping> <filter-name>guiceFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
MyJerseyGuiceServletContextListener.java
In this example, we use the servlet context listener named JerseyGuiceServletContextListener
which comes from jersey2-guice
artifact as parent class.
public class MyJerseyGuiceServletContextListener extends JerseyGuiceServletContextListener { @Override protected List<? extends Module> modules() { return Collections.singletonList(new ServletModule() { @Override protected void configureServlets() { bind(MyService.class).to(MyServiceImpl.class); } }); } }
Service class
We use very simple pair of an interface and implementation that creates a simple greeting message, which used in a past entry so omitted for simplicity.
MyResource.java
This is an simple implementation of JAX-RS resource class. placed under guice.tomcat.jersey
package. the preceding service class named MyService
will be injected by @javax.inject.Inject
annotation.
@Path("myresource") public class MyResource { @Inject private MyService myService; @GET @Produces(MediaType.TEXT_PLAIN) public String getIt() { return myService.hello("Jersey"); } }
Test run
$ curl http://localhost:8080/webapi/myresource Hello, Jersey