RabbitMQ播放模块! 构架
這個(gè)新模塊允許您在Play的RabbitMQ實(shí)例上使用和產(chǎn)生消息! 框架應(yīng)用程序。
安裝
play install rabbitmq組態(tài)
module.rabbitmq=${play.path}/modules/rabbitmq-0.0.1 rabbitmq.host=localhost rabbitmq.port=5672 rabbitmq.userName=guest rabbitmq.password=guest rabbitmq.vhost=/ rabbitmq.exchangeType=direct rabbitmq.durable=true rabbitmq.autoAck=false rabbitmq.basicQos=true定義將由隊(duì)列使用的消息(只是一個(gè)簡(jiǎn)單的POJO)
public class SampleMessage implements Serializable {/** The field1. */private String field1;/** The field2. */private String field2;/*** Instantiates a new sample message.*/public SampleMessage() {}/*** Instantiates a new sample message.** @param field1 the field1* @param field2 the field2*/public SampleMessage(String field1, String field2) {super();this.field1 = field1;this.field2 = field2;}/*** Gets the field1.** @return the field1*/public String getField1() {return field1;}/*** Sets the field1.** @param field1 the new field1*/public void setField1(String field1) {this.field1 = field1;}/*** Gets the field2.** @return the field2*/public String getField2() {return field2;}/*** Sets the field2.** @param field2 the new field2*/public void setField2(String field2) {this.field2 = field2;}/*** To String** @see java.lang.Object#toString()*/@Overridepublic String toString() {return "SampleMessage [field1=" + field1 + ", field2=" + field2 + "]";} }發(fā)布消息
public static void publish(String q) {RabbitMQPublisher.publish("myQueue", new SampleMessage(q, q));render(q);}創(chuàng)建消息使用者
@OnApplicationStart(async=true) public class RabbitMQSampleConsumer extends RabbitMQConsumer {/*** Consume Message** @see play.modules.rabbitmq.consumer.RabbitMQConsumer#consume(T)*/@Overrideprotected void consume(SampleMessage message) {System.out.println("******************************");System.out.println("* Message Consumed: " + message);System.out.println("******************************");}/*** Name of the Queue that this consumer will be listening to.** @return the string* @see play.modules.rabbitmq.consumer.RabbitMQConsumer#queue()*/@Overrideprotected String queue() {return "myQueue";}/*** Return message type.** @return the message type* @see play.modules.rabbitmq.consumer.RabbitMQConsumer#getMessageType()*/protected Class getMessageType() {return SampleMessage.class;} }*請(qǐng)注意,這是一場(chǎng)戲! 作業(yè),因此您可以手動(dòng)啟動(dòng)它,也可以使用Play提供的其他注釋! 例如@On或@Every。 有關(guān)更多信息,請(qǐng)參見(jiàn)“ 異步作業(yè)”文檔 。
Firehose –另一種批量發(fā)布消息的方法
@OnApplicationStart(async = true) public class RabbitMQSampleFirehose extends RabbitMQFirehose {/** The count. */public int count = 0;/*** Get data to be loaded.** @param n the n* @return the data* @throws Exception the exception* @see play.modules.rabbitmq.producer.RabbitMQFirehose#getData(int)*/@Overrideprotected List getData(int n) throws Exception {if ( count >= 10 ) {return null;}List results = new ArrayList();for (int i = 0; i < n; i++) {results.add(new SampleMessage("field1", "field2"));count++;}return results;}/*** Batch Size - How many records we will select at the time?.** @return the int* @see play.modules.rabbitmq.producer.RabbitMQFirehose#batchSize()*/@Overrideprotected int batchSize() {return 2;}/*** Queue Name.** @return the string* @see play.modules.rabbitmq.producer.RabbitMQFirehose#queueName()*/@Overrideprotected String queueName() {return "myQueue";}}*請(qǐng)注意,這是一場(chǎng)戲! 作業(yè),因此您可以手動(dòng)啟動(dòng)它,也可以使用Play提供的其他注釋! 例如@On或@Every。 有關(guān)更多信息,請(qǐng)參見(jiàn)“ 異步作業(yè)”文檔 。 當(dāng)然,該代碼可在Github上獲得 。
現(xiàn)在開(kāi)始游戲!
參考: RabbitMQ Play模塊! 來(lái)自JCG合作伙伴 Felipe Oliveira在Geeks的 框架 完全在 。
相關(guān)文章:
- Java Code Geeks Andygene Web原型
- 每個(gè)程序員都應(yīng)該知道的事情
- Spring MVC開(kāi)發(fā)–快速教程
- SmartGWT入門(mén),提供出色的GWT界面
- GWT 2 Spring 3 JPA 2 Hibernate 3.5教程
翻譯自: https://www.javacodegeeks.com/2011/04/rabbitmq-module-play-framework.html
總結(jié)
以上是生活随笔為你收集整理的RabbitMQ播放模块! 构架的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ddos软件教程(制作ddos软件下载)
- 下一篇: Xuggler开发教程