-
Notifications
You must be signed in to change notification settings - Fork 166
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
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 6 additions & 1 deletion
.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
java/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
24 changes: 24 additions & 0 deletions
java/src/main/java/rabbitmqinaction/sourcecode/GenericConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>(); | ||
|
|
||
| } |
2 changes: 2 additions & 0 deletions
java/appendix-a/Client.java → ...inaction/sourcecode/appendixA/Client.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
java/src/main/java/rabbitmqinaction/sourcecode/appendixA/Server.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
java/src/main/java/rabbitmqinaction/sourcecode/chapter2/Chapter2Configuration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
|
|
||
| } |
61 changes: 61 additions & 0 deletions
java/src/main/java/rabbitmqinaction/sourcecode/chapter2/Consumer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ); | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.