Skip to content

Commit

Permalink
added some producer and consumer code going to run activemq
Browse files Browse the repository at this point in the history
  • Loading branch information
RaymondArias committed Nov 8, 2016
1 parent 26c85bc commit 2be0ff3
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 0 deletions.
118 changes: 118 additions & 0 deletions ScalaChatApp/.cache-main

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions ScalaChatApp/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="/opt/apache-activemq-5.14.1/activemq-all-5.14.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions ScalaChatApp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
18 changes: 18 additions & 0 deletions ScalaChatApp/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ScalaChatApp</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.scala-ide.sdt.core.scalabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.scala-ide.sdt.core.scalanature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions ScalaChatApp/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
49 changes: 49 additions & 0 deletions ScalaChatApp/src/Consumer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import javax.jms._
import org.apache.activemq.ActiveMQConnection
import org.apache.activemq.ActiveMQConnectionFactory


object Consumer {
val url = "tcp://localhost:61616"
val topic = "chatroom"

def main(args: Array[String])
{
val connectionFactory = new ActiveMQConnectionFactory(url)
val connection = connectionFactory.createConnection
//connection.setClientID("ConsumerSynchronous")
connection.start()

println("Started")

val session: Session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)
//val queue = session.createQueue("SendSynchronousMsgQueue")
val destination: Topic = session.createTopic(this.topic)
val consumer: MessageConsumer = session.createConsumer(destination)

val listener = new MessageListener {
def onMessage(message: Message) {
message match {
case text: TextMessage => {
//val replyProducer = session.cr
//replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT)

println("Received message: " + text.getText)

//val replyMessage = session.createTextMessage("Yes I received your message!")
//replyMessage.setJMSCorrelationID(text.getJMSCorrelationID())

//println("Reply sent!")

//replyProducer.send(replyMessage)
}
case _ => {
throw new Exception("Unhandled Message Type: " + message.getClass.getSimpleName)
}
}
}
}
consumer.setMessageListener(listener)
}

}
28 changes: 28 additions & 0 deletions ScalaChatApp/src/Producer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory
import javax.jms._
import java.util.Scanner
object Producer {
val url: String = "tcp://localhost:61616"
val topicName: String = "chatroom"

def main(args:Array[String])
{
val input: Scanner = new Scanner(System.in)
val connectionFactory: ConnectionFactory = new ActiveMQConnectionFactory(url)
val connection: Connection = connectionFactory.createConnection
connection.start
val session: Session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)
val destination: Topic = session.createTopic(topicName)
val messageProducer: MessageProducer = session.createProducer(destination)
val textMessage: TextMessage = session.createTextMessage("Hello Subscriber!")
while(true) {
val userMessage = input.nextLine()
textMessage.setText(userMessage)
messageProducer.send(textMessage)
println("Message sent to subscriber: '" + textMessage.getText + "'")
}
connection.close

}
}

0 comments on commit 2be0ff3

Please sign in to comment.