Listener(): Unit = {
val connectionFactory = new ActiveMQConnectionFactory(activeMqUrl)
val connection = connectionFactory.createConnection
connection.setClientID("gatling")
connection.start
println("Started")
val qsession: javax.jms.Session = connection.createSession(false, ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE)
val queue = qsession.createQueue("dest")
val consumer = qsession.createConsumer(queue)
var count = 0
var msg : String = ""
val listener = new MessageListener {
def onMessage(message: Message) {
var msg1 = ""
message match {
case text: BytesMessage => {
if (message.isInstanceOf[BytesMessage] && count < 1) {
val byteMessage = message.asInstanceOf[BytesMessage]
var byteData = new Array[Byte](byteMessage.getBodyLength.toInt)
byteMessage.readBytes(byteData)
byteMessage.reset()
var stringMessage = new String(byteData)
// println("Received message: " + stringMessage)
count += 1
println("Count: " + count)
text.acknowledge()
msg1 = stringMessage
// println(msg)
}
}
case _ => {
throw new Exception("Unhandled Message Type: " + message.getClass.getSimpleName)
}
println("msg1 :" + msg1)
}
}
}
consumer.setMessageListener(listener)
}
scastie.scala-lang.org
def Listner(callback: String => Unit) и внутри вызови в конце callback
Обсуждают сегодня