Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chapter two java examples #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sifraser wants to merge 3 commits into rabbitinaction:master
base: master
Choose a base branch
Loading
from siniatech:java_examples
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
bin
target
obj
.project
.cache
.settings
.classpath
*.suo
*.csproj.user
*.class
*.jar
*.jar
102 changes: 0 additions & 102 deletions java/appendix-a/Server.java
View file Open in desktop

This file was deleted.

35 changes: 35 additions & 0 deletions java/pom.xml
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>rabbitmqinaction</groupId>
<artifactId>sourcecode-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sourcecode-java</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
</dependencies>
</project>
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* RabbitMQ in Action - Generic constants
*
* @author Simon Fraser, Siniatech Ltd
*/
package rabbitmqinaction.sourcecode;

import java.util.HashMap;
import java.util.Map;

public class GenericConfiguration {

public static final boolean ACTIVE = false;
public static final boolean PASSIVE = false;
public static final boolean DURABLE = true;
public static final boolean NON_DURABLE = false;
public static final boolean AUTO_DELETE = true;
public static final boolean NON_AUTO_DELETE = false;
public static final String DIRECT_EXCHANGE_TYPE = "direct";
public static final String HOST = "localhost";
public static final String PLAIN_CONTENT_TYPE = "text/plain";
public static final Map<String, Object> EMPTY_MAP = new HashMap<String, Object>();

}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package rabbitmqinaction.sourcecode.appendixA;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package rabbitmqinaction.sourcecode.appendixA;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.rabbitmq.client.AMQP.BasicProperties;
import org.json.JSONObject;

public class Server {
private Connection connection;
private Channel channel;
private QueueingConsumer consumer;

public Server init() throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername( "rpc_user" );
factory.setPassword( "rpcme" );
connection = factory.newConnection();
channel = connection.createChannel();

channel.exchangeDeclare( "rpc", "direct" );
channel.queueDeclare( "ping", false, false, false, null );
channel.queueBind( "ping", "rpc", "ping" );

consumer = new QueueingConsumer( channel );
channel.basicConsume( "ping", false, "ping", consumer );

System.out.println( "Waiting for RPC calls..." );

return this;
}

public void closeConnection() {
if ( connection != null ) {
try {
connection.close();
} catch ( Exception ignore ) {
}
}
}

public void serveRequests() {
while ( true ) {
try {

Delivery delivery = consumer.nextDelivery();
BasicProperties props = delivery.getProperties();

channel.basicAck( delivery.getEnvelope().getDeliveryTag(), false );
System.out.println( "Received API call...replying..." );

channel.basicPublish( "", props.getReplyTo(), null, getResponse( delivery ).getBytes( "UTF-8" ) );

} catch ( Exception e ) {
System.out.println( e.toString() );
}
}
}

private String getResponse( Delivery delivery ) {
String response = null;
try {
String message = new String( delivery.getBody(), "UTF-8" );
JSONObject jsonobject = new JSONObject( message );
response = "Pong!" + jsonobject.getString( "time" );
} catch ( Exception e ) {
System.out.println( e.toString() );
response = "";
}
return response;
}

public static void main( String[] args ) {
Server server = null;
try {
server = new Server();
server.init().serveRequests();
} catch ( Exception e ) {
e.printStackTrace();
} finally {
if ( server != null ) {
server.closeConnection();
}
}
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* RabbitMQ in Action - Chapter 2 Examples
*
* @author Alvaro Videla (original)
* @author Jason J. W. Williams (original)
* @author Simon Fraser, Siniatech Ltd (translation)
*/
package rabbitmqinaction.sourcecode.chapter2;

public class Chapter2Configuration {

public static final String ROUTING_KEY = "hola";
public static final String USERNAME = "guest";
public static final String PASSWORD = "guest";
public static final String EXCHANGE = "hello-exchange";
public static final String QUEUE_NAME = "hello-queue";
public static final String CONSUMER_TAG = "hello-consumer";

}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* RabbitMQ in Action - Chapter 2 Examples
*
* @author Alvaro Videla (original)
* @author Jason J. W. Williams (original)
* @author Simon Fraser, Siniatech Ltd (translation)
*/
package rabbitmqinaction.sourcecode.chapter2;

import static rabbitmqinaction.sourcecode.GenericConfiguration.*;
import static rabbitmqinaction.sourcecode.chapter2.Chapter2Configuration.*;

import java.io.IOException;
import java.util.HashMap;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Consumer {

public static void main( String[] args ) throws IOException {
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername( USERNAME );
factory.setPassword( PASSWORD );
factory.setHost( HOST );

Connection connection = factory.newConnection();

Channel channel = connection.createChannel();
channel.exchangeDeclare( EXCHANGE, DIRECT_EXCHANGE_TYPE, ACTIVE, DURABLE, NON_AUTO_DELETE, new HashMap<String, Object>() );
channel.queueDeclare( QUEUE_NAME, ACTIVE, DURABLE, NON_AUTO_DELETE, null );
channel.queueBind( QUEUE_NAME, EXCHANGE, ROUTING_KEY, EMPTY_MAP );
channel.basicConsume( QUEUE_NAME, false, CONSUMER_TAG, new ConsumerCallback( channel ) );
}

}

class ConsumerCallback extends DefaultConsumer {

public ConsumerCallback( Channel channel ) {
super( channel );
}

@Override
public void handleDelivery( String consumerTag, Envelope envelope, BasicProperties properties, byte[] body ) throws IOException {
String msg = new String( body );
if ( "quit".equals( msg ) ) {
getChannel().basicCancel( consumerTag );
getChannel().close();
getChannel().getConnection().close();
System.exit( 0 );
} else {
System.out.println( msg );
}
}

}
Loading

AltStyle によって変換されたページ (->オリジナル) /