Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
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
127 changes: 127 additions & 0 deletions scala/pom.xml
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>

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"
}
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"
}
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);
}
}
}
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
Copy link
Copy Markdown
Collaborator

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

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");
}
}