Play Framework通过Rabbit MQ发送消息 [英] Play Framework send message via rabbit mq

查看:121
本文介绍了Play Framework通过Rabbit MQ发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人在Play Framework上使用Rabbitmq吗?

Does anybody out there using rabbitmq with Play Framework?

我有一个使用Play Framework用Scala编写的AI.我有一个处理nosql数据库的平均堆栈. 我想通过Rabbitmq向该AI发送JSON消息.

I have an AI written in Scala using Play Framework. And I have a mean stack, which handles a nosql database. I'd like to send json messages via rabbitmq to that AI.

我已经可以使用nodejs和Rabbitmq进行操作,但是现在我想连接游戏,可能需要您的帮助.

I already got things going with nodejs and rabbitmq, but now that i want to connect to play I might need your help.

有人在Rabbitmq上玩过游戏或有任何实际建议吗?

Has someone any experience with rabbitmq and play or some practical Advices?

谢谢!

推荐答案

您可以使用标准Java库将消息发送到RabbitMQ,如下所示:

You can use the standard Java library to send messages to RabbitMQ like this:

val factory = new ConnectionFactory()
factory.setUri(serverUri)
val channel = connection.createChannel()
channel.queueDeclare(queue, true, false, false, null)
channel.basicPublish("", queue, true, false, null, data)

在Play中使用此代码时,您可能会将所有逻辑放到basicPublish自己的类中,并且将该实例注入您的Controller ,以便您可以使用与Rabbit的持久连接.

When using this in Play, you'd probably put all the logic up to basicPublish inside its own class and inject that instance into your Controller, so that you can use a persistent connection to Rabbit.

由于您似乎要做的就是从请求中获取json并将其推送到Rabbit中,因此您甚至不必处理序列化,只需从Request中获取文本主体并将其作为data basicPublish的有效负载(除非您想在传递输入之前执行输入验证).

Since all your seem to be doing is taking json from a request and pushing it into Rabbit, you don't even have to deal with serialization and can just take the text body from the Request and pass it as the data payload of basicPublish (unless you want to perform validation of the input before passing it on).

这篇关于Play Framework通过Rabbit MQ发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆