add drill 07

This commit is contained in:
Raffaele Mignone 2020-04-22 11:59:55 +02:00
parent d8f4e8bedf
commit 10bf3e3588
Signed by: norangebit
GPG Key ID: F5255658CB220573
16 changed files with 404 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package drills.drill07.exercise1.point1
import org.apache.activemq.ActiveMQConnectionFactory
import javax.jms.QueueConnectionFactory
import javax.jms.Session
fun main() {
val url = "tcp://localhost:61616"
val factory: QueueConnectionFactory = ActiveMQConnectionFactory(url)
val connection = factory.createQueueConnection()
connection.start()
val session = connection.createQueueSession(
false,
Session.CLIENT_ACKNOWLEDGE
)
val queue = session.createQueue("ack")
val receiver = session.createReceiver(queue)
repeat(100) {
val msg = receiver.receive()
val value = msg.getIntProperty("value")
println(value)
msg.acknowledge()
}
session.close()
connection.close()
}

View File

@ -0,0 +1,29 @@
package drills.drill07.exercise1.point1
import org.apache.activemq.ActiveMQConnectionFactory
import javax.jms.QueueConnectionFactory
import javax.jms.Session
fun main() {
val url = "tcp://localhost:61616"
val factory: QueueConnectionFactory = ActiveMQConnectionFactory(url)
val connection = factory.createQueueConnection()
val session = connection.createQueueSession(
false,
Session.AUTO_ACKNOWLEDGE
)
val queue = session.createQueue("ack")
val sender = session.createSender(queue)
val msg = session.createMessage()
repeat(100) {
msg.setIntProperty("value", it)
sender.send(msg)
}
session.close()
connection.close()
}

View File

@ -0,0 +1,41 @@
package drills.drill07.exercise1.point2
import org.apache.activemq.ActiveMQConnectionFactory
import javax.jms.QueueConnectionFactory
import javax.jms.Session
fun main() {
val url = "tcp://localhost:61616"
val factory: QueueConnectionFactory = ActiveMQConnectionFactory(url)
val connection = factory.createQueueConnection()
connection.start()
val session = connection.createQueueSession(
true,
Session.SESSION_TRANSACTED
)
val queue = session.createQueue("transacted")
val receiver = session.createReceiver(queue)
repeat(10) {
val msg = receiver.receive()
val value = msg.getIntProperty("value")
println(value)
}
println("retry")
session.rollback()
repeat(10) {
val msg = receiver.receive()
val value = msg.getIntProperty("value")
println(value)
}
session.commit()
session.close()
connection.close()
}

View File

@ -0,0 +1,31 @@
package drills.drill07.exercise1.point2
import org.apache.activemq.ActiveMQConnectionFactory
import javax.jms.QueueConnectionFactory
import javax.jms.Session
fun main() {
val url = "tcp://localhost:61616"
val factory: QueueConnectionFactory = ActiveMQConnectionFactory(url)
val connection = factory.createQueueConnection()
val session = connection.createQueueSession(
true,
Session.SESSION_TRANSACTED
)
val queue = session.createQueue("transacted")
val sender = session.createSender(queue)
val msg = session.createMessage()
repeat(10) {
msg.setIntProperty("value", it)
sender.send(msg)
}
session.commit()
session.close()
connection.close()
}

View File

@ -0,0 +1,35 @@
package drills.drill07.exercise2
import org.apache.activemq.ActiveMQConnectionFactory
import javax.jms.Queue
import javax.jms.QueueConnectionFactory
import javax.jms.Session
import javax.jms.TextMessage
fun main() {
val url = "tcp://localhost:61616"
val factory: QueueConnectionFactory = ActiveMQConnectionFactory(url)
val connection = factory.createQueueConnection()
connection.start()
val session = connection.createQueueSession(
false,
Session.AUTO_ACKNOWLEDGE
)
val queue = session.createQueue("requestor")
val receiver = session.createReceiver(queue)
val msg = receiver.receive() as TextMessage
println(msg.text)
val replyQueue = msg.jmsReplyTo as Queue
val sender = session.createSender(replyQueue)
val replyMsg = session.createTextMessage("Echo ${msg.text}")
sender.send(replyMsg)
session.close()
connection.close()
}

View File

@ -0,0 +1,35 @@
package drills.drill07.exercise2
import org.apache.activemq.ActiveMQConnectionFactory
import javax.jms.QueueConnectionFactory
import javax.jms.Session
import javax.jms.TextMessage
fun main() {
val url = "tcp://localhost:61616"
val factory: QueueConnectionFactory = ActiveMQConnectionFactory(url)
val connection = factory.createQueueConnection()
connection.start()
val session = connection.createQueueSession(
false,
Session.AUTO_ACKNOWLEDGE
)
val queue = session.createQueue("requestor")
val sender = session.createSender(queue)
val tmpQueue = session.createTemporaryQueue()
val receiver = session.createReceiver(tmpQueue)
val msg = session.createTextMessage("Hello Home!")
msg.jmsReplyTo = tmpQueue
sender.send(msg)
val replyMsg = receiver.receive() as TextMessage
println(replyMsg.text)
session.close()
connection.close()
}

View File

@ -0,0 +1,17 @@
package drills.drill07.exercise3.hello
import drills.drill07.exercise3.replier.ActiveMQReplier
import drills.drill07.exercise3.replier.Replier
import javax.jms.TextMessage
fun main() {
val replier: Replier = ActiveMQReplier("hello")
replier.start()
replier.onRequest {
val msg = it as TextMessage
println(msg.text)
replier.createTextMessage("Echo ${msg.text}")
}
}

View File

@ -0,0 +1,15 @@
package drills.drill07.exercise3.hello
import drills.drill07.exercise3.requestor.ActiveMQRequestor
import drills.drill07.exercise3.requestor.Requestor
import javax.jms.TextMessage
fun main() {
val requestor: Requestor = ActiveMQRequestor("hello")
val msg = requestor.request(requestor.createTextMessage("Hello Home!")) as TextMessage
println(msg.text)
requestor.close()
}

View File

@ -0,0 +1,44 @@
package drills.drill07.exercise3.replier
import javax.jms.Message
import javax.jms.Queue
import javax.jms.QueueConnectionFactory
import javax.jms.Session
import javax.jms.ObjectMessage
import javax.jms.TextMessage
abstract class AbstarctReplier(val queueName: String) : Replier {
private val factory: QueueConnectionFactory = this.getFactory()
private val connection = factory.createQueueConnection()
private val session = connection.createQueueSession(
false,
Session.AUTO_ACKNOWLEDGE
)
private val queue = session.createQueue(queueName)
private val receiver = session.createReceiver(queue)
override fun onRequest(buildResponse: (Message) -> Message) {
receiver.setMessageListener {
val replyQueue = it.jmsReplyTo as Queue
val sender = session.createSender(replyQueue)
sender.send(buildResponse(it))
}
}
override fun createTextMessage(text: String): TextMessage = session.createTextMessage(text)
override fun createObjectMessage(): ObjectMessage = session.createObjectMessage()
override fun createMessage(): Message = session.createMessage()
override fun start() {
connection.start()
}
override fun close() {
session.close()
connection.close()
}
abstract fun getFactory(uri: String = "tcp://localhost:61616"): QueueConnectionFactory
}

View File

@ -0,0 +1,8 @@
package drills.drill07.exercise3.replier
import org.apache.activemq.ActiveMQConnectionFactory
import javax.jms.QueueConnectionFactory
class ActiveMQReplier(queueName: String) : AbstarctReplier(queueName) {
override fun getFactory(uri: String): QueueConnectionFactory = ActiveMQConnectionFactory(uri)
}

View File

@ -0,0 +1,14 @@
package drills.drill07.exercise3.replier
import javax.jms.Message
import javax.jms.ObjectMessage
import javax.jms.TextMessage
interface Replier {
fun onRequest(buildResponse: (Message) -> Message)
fun createTextMessage(text: String = ""): TextMessage
fun createObjectMessage(): ObjectMessage
fun createMessage(): Message
fun start()
fun close()
}

View File

@ -0,0 +1,42 @@
package drills.drill07.exercise3.requestor
import javax.jms.Message
import javax.jms.QueueConnectionFactory
import javax.jms.Session
import javax.jms.ObjectMessage
import javax.jms.TextMessage
abstract class AbstractRequestor(val queueName: String) : Requestor {
private val connectionFactory: QueueConnectionFactory = this.getFactory()
private val connection = connectionFactory.createQueueConnection()
.also { it.start() }
private val session = connection.createQueueSession(
false,
Session.AUTO_ACKNOWLEDGE
)
private val queue = session.createQueue(queueName)
private val sender = session.createSender(queue)
override fun request(message: Message): Message {
val tmpQueue = session.createTemporaryQueue()
message.jmsReplyTo = tmpQueue
sender.send(message)
val tmpReceiver = session.createReceiver(tmpQueue)
return tmpReceiver.receive()
}
override fun createTextMessage(text: String): TextMessage = session.createTextMessage(text)
override fun createObjectMessage(): ObjectMessage = session.createObjectMessage()
override fun createMessage(): Message = session.createMessage()
override fun close() {
session.close()
connection.close()
}
abstract fun getFactory(uri: String = "tcp://localhost:61616"): QueueConnectionFactory
}

View File

@ -0,0 +1,8 @@
package drills.drill07.exercise3.requestor
import org.apache.activemq.ActiveMQConnectionFactory
import javax.jms.QueueConnectionFactory
class ActiveMQRequestor(queueName: String) : AbstractRequestor(queueName) {
override fun getFactory(uri: String): QueueConnectionFactory = ActiveMQConnectionFactory(uri)
}

View File

@ -0,0 +1,13 @@
package drills.drill07.exercise3.requestor
import javax.jms.Message
import javax.jms.ObjectMessage
import javax.jms.TextMessage
interface Requestor {
fun request(message: Message): Message
fun createTextMessage(text: String = ""): TextMessage
fun createObjectMessage(): ObjectMessage
fun createMessage(): Message
fun close()
}

View File

@ -0,0 +1,21 @@
package drills.drill07.exercise4
import drills.drill07.exercise3.replier.ActiveMQReplier
import drills.drill07.exercise3.replier.Replier
fun main() {
val replier: Replier = ActiveMQReplier("hello")
replier.start()
replier.onRequest {
val n = it.getIntProperty("fact")
val resultMessage = replier.createMessage()
resultMessage.setLongProperty("result", fact(n))
resultMessage
}
}
private fun fact(n: Int): Long {
return if (n <= 1) 1 else n * fact(n - 1)
}

View File

@ -0,0 +1,20 @@
package drills.drill07.exercise4
import drills.drill07.exercise3.requestor.ActiveMQRequestor
import drills.drill07.exercise3.requestor.Requestor
fun main() {
val requestor: Requestor = ActiveMQRequestor("hello")
println("Enter the number of which you want to calculate the factorial:")
val n = readLine()?.toInt()
val sendMessage = requestor.createMessage()
.also { it.setIntProperty("fact", n!!) }
val replyMessage = requestor.request(sendMessage)
println("The factorial of $n is ${replyMessage.getLongProperty("result")}")
requestor.close()
}