-
Notifications
You must be signed in to change notification settings - Fork 168
chapter 2 scala examples #7
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
Choose a base branch
from
siniatech:scala_examples
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 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
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 |
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,127 @@ | ||
| <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-scala</artifactId> | ||
| <version>0.0.1-SNAPSHOT</version> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <name>sourcecode-scala</name> | ||
| <url>http://maven.apache.org</url> | ||
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <scala.version>2.9.1</scala.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.scala-lang</groupId> | ||
| <artifactId>scala-library</artifactId> | ||
| <version>${scala.version}</version> | ||
| </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> | ||
| <!-- Test --> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.8.1</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.scalatest</groupId> | ||
| <artifactId>scalatest</artifactId> | ||
| <version>1.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| </dependencies> | ||
|
|
||
| <build> | ||
| <sourceDirectory>src/main/scala</sourceDirectory> | ||
| <testSourceDirectory>src/test/scala</testSourceDirectory> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.scala-tools</groupId> | ||
| <artifactId>maven-scala-plugin</artifactId> | ||
| <version>2.15.0</version> | ||
| <executions> | ||
| <execution> | ||
| <goals> | ||
| <goal>compile</goal> | ||
| <goal>testCompile</goal> | ||
| </goals> | ||
| <configuration> | ||
| <args> | ||
| <arg>-make:transitive</arg> | ||
| <arg>-dependencyfile</arg> | ||
| <arg>${project.build.directory}/.scala_dependencies</arg> | ||
| </args> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>2.6</version> | ||
| <configuration> | ||
| <useFile>false</useFile> | ||
| <disableXmlReport>true</disableXmlReport> | ||
| <!-- If you have classpath issue like NoDefClassError,... --> | ||
| <!-- useManifestOnlyJar>false</useManifestOnlyJar --> | ||
| <includes> | ||
| <include>**/*Test.*</include> | ||
| <include>**/*Suite.*</include> | ||
| </includes> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| <pluginManagement> | ||
| <plugins> | ||
| <!--This plugin's configuration is used to store Eclipse m2e settings | ||
| only. It has no influence on the Maven build itself. --> | ||
| <plugin> | ||
| <groupId>org.eclipse.m2e</groupId> | ||
| <artifactId>lifecycle-mapping</artifactId> | ||
| <version>1.0.0</version> | ||
| <configuration> | ||
| <lifecycleMappingMetadata> | ||
| <pluginExecutions> | ||
| <pluginExecution> | ||
| <pluginExecutionFilter> | ||
| <groupId>org.scala-tools</groupId> | ||
| <artifactId> | ||
| maven-scala-plugin | ||
| </artifactId> | ||
| <versionRange> | ||
| [2.15.0,) | ||
| </versionRange> | ||
| <goals> | ||
| <goal>compile</goal> | ||
| <goal>testCompile</goal> | ||
| </goals> | ||
| </pluginExecutionFilter> | ||
| <action> | ||
| <ignore></ignore> | ||
| </action> | ||
| </pluginExecution> | ||
| </pluginExecutions> | ||
| </lifecycleMappingMetadata> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </pluginManagement> | ||
| </build> | ||
| </project> | ||
|
|
19 changes: 19 additions & 0 deletions
19
scala/src/main/scala/rabbitmqinaction/sourcecode/GenericConfiguration.scala
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 - Generic constants | ||
| * | ||
| * @author Simon Fraser, Siniatech Ltd | ||
| */ | ||
| package rabbitmqinaction.sourcecode | ||
|
|
||
| object GenericConfiguration { | ||
| val Active = false | ||
| val Passive = false | ||
| val Durable = true | ||
| val NonDurable = false | ||
| val AutoDelete = true | ||
| val NonAutoDelete = false | ||
| val DirectExchangeType = "direct" | ||
| val PlainContentType = "text/plain" | ||
| val EmptyJavaMap = new java.util.HashMap[String, Object] | ||
| def host: String = "localhost" | ||
| } |
17 changes: 17 additions & 0 deletions
17
scala/src/main/scala/rabbitmqinaction/sourcecode/chapter2/Chapter2Configuration.scala
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,17 @@ | ||
| /** | ||
| * 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 | ||
|
|
||
| object Chapter2Configuration { | ||
| val RoutingKey = "hola" | ||
| val Username = "guest" | ||
| val Password = "guest" | ||
| val Exchange = "hello-exchange" | ||
| val QueueName = "hello-queue" | ||
| val ConsumerTag = "hello-consumer" | ||
| } |
60 changes: 60 additions & 0 deletions
60
scala/src/main/scala/rabbitmqinaction/sourcecode/chapter2/Consumer.scala
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,60 @@ | ||
| /** | ||
| * 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 com.rabbitmq.client.AMQP.BasicProperties | ||
| import com.rabbitmq.client.Channel | ||
| import com.rabbitmq.client.ConnectionFactory | ||
| import com.rabbitmq.client.DefaultConsumer | ||
| import com.rabbitmq.client.Envelope | ||
|
|
||
| import Chapter2Configuration.ConsumerTag | ||
| import Chapter2Configuration.Exchange | ||
| import Chapter2Configuration.Password | ||
| import Chapter2Configuration.QueueName | ||
| import Chapter2Configuration.RoutingKey | ||
| import Chapter2Configuration.Username | ||
| import rabbitmqinaction.sourcecode.GenericConfiguration.Active | ||
| import rabbitmqinaction.sourcecode.GenericConfiguration.DirectExchangeType | ||
| import rabbitmqinaction.sourcecode.GenericConfiguration.Durable | ||
| import rabbitmqinaction.sourcecode.GenericConfiguration.EmptyJavaMap | ||
| import rabbitmqinaction.sourcecode.GenericConfiguration.NonAutoDelete | ||
| import rabbitmqinaction.sourcecode.GenericConfiguration.host | ||
|
|
||
| object Consumer { | ||
| def main(args: Array[String]) { | ||
| val factory = new ConnectionFactory | ||
| factory.setUsername(Username) | ||
| factory.setPassword(Password) | ||
| factory.setHost(host) | ||
|
|
||
| val connection = factory.newConnection(); | ||
|
|
||
| val channel = connection.createChannel(); | ||
| channel.exchangeDeclare(Exchange, DirectExchangeType, Active, Durable, NonAutoDelete, EmptyJavaMap); | ||
| channel.queueDeclare(QueueName, Active, Durable, NonAutoDelete, null); | ||
| channel.queueBind(QueueName, Exchange, RoutingKey, EmptyJavaMap); | ||
| channel.basicConsume(QueueName, false, ConsumerTag, new ConsumerCallback(channel)); | ||
| } | ||
| } | ||
|
|
||
| class ConsumerCallback(channel: Channel) extends DefaultConsumer(channel) { | ||
| override def handleDelivery(consumerTag: String, envelope: Envelope, props: BasicProperties, body: Array[Byte]) { | ||
| getChannel.basicAck(envelope.getDeliveryTag, false) | ||
| val msg = new String(body) | ||
| msg match { | ||
| case "quit" => { | ||
| getChannel.basicCancel(consumerTag) | ||
| getChannel.close | ||
| getChannel.getConnection.close | ||
| System.exit(0) | ||
| } | ||
| case _ => println(msg); | ||
| } | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
scala/src/main/scala/rabbitmqinaction/sourcecode/chapter2/Producer.scala
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,64 @@ | ||
| /** | ||
| * 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 com.rabbitmq.client.AMQP | ||
| import com.rabbitmq.client.ConfirmListener | ||
| import com.rabbitmq.client.ConnectionFactory | ||
|
|
||
| import Chapter2Configuration.Exchange | ||
| import Chapter2Configuration.Password | ||
| import Chapter2Configuration.RoutingKey | ||
| import Chapter2Configuration.Username | ||
| import rabbitmqinaction.sourcecode.GenericConfiguration.PlainContentType | ||
| import rabbitmqinaction.sourcecode.GenericConfiguration.host | ||
|
|
||
| object Producer { | ||
|
|
||
| def main(args: Array[String]) { | ||
| if (args.length != 1) { | ||
| System.err.println("Message body must be supplied"); | ||
| System.exit(1); | ||
| } | ||
|
|
||
| val factory = new ConnectionFactory | ||
| factory.setUsername(Username) | ||
| factory.setPassword(Password) | ||
| factory.setHost(host) | ||
|
|
||
| val connection = factory.newConnection | ||
|
|
||
| val msg = args.head | ||
| val msgPropertiesBuilder = new AMQP.BasicProperties.Builder | ||
| msgPropertiesBuilder.contentType(PlainContentType) | ||
| val msgProperties = msgPropertiesBuilder.build | ||
|
|
||
| val channel = connection.createChannel | ||
| channel.confirmSelect | ||
| channel.addConfirmListener(ConfirmHandler); | ||
| channel.basicPublish(Exchange, RoutingKey, msgProperties, msg.getBytes) | ||
| channel.waitForConfirmsOrDie | ||
|
|
||
| channel.close | ||
| connection.close | ||
| } | ||
| } | ||
|
|
||
| object ConfirmHandler extends ConfirmListener { | ||
| override def handleAck(deliveryTag: Long, multiple: Boolean) { | ||
| println("Confirm receipt") | ||
| } | ||
|
|
||
| override def handleNack(deliveryTag: Long, multiple: Boolean) { | ||
| println("Message lost"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think basic Producer from ch2 is not doing confirm selects. If your intention was to implement this https://github.com/rabbitinaction/sourcecode/blob/master/python/chapter-2/hello_world_producer_pubconfirm.py then you might want to rename the class/file