RabbitMQ提供了具有可預測且一致的吞吐量和延遲的高可用性,可伸縮和便攜式消息系統。 RabbitMQ是AMQP (業務消息傳遞的開放標準)的領先實現 ,并且通過適配器支持XMPP,SMTP,STOMP和HTTP來進行輕量級Web消息傳遞。
這個新模塊允許您在Play的RabbitMQ實例上使用和產生消息! 框架應用程序。
安裝
play install rabbitmq
組態
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
定義將由隊列使用的消息(只是一個簡單的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 + "]";}
}
發布消息
public static void publish(String q) {RabbitMQPublisher.publish("myQueue", new SampleMessage(q, q));render(q);}
創建消息使用者
@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;}
}
*請注意,這是一場戲! 作業,因此您可以手動啟動它,也可以使用Play提供的其他注釋! 例如@On或@Every。 有關更多信息,請參見“ 異步作業”文檔 。
Firehose –另一種批量發布消息的方法
@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";}}
*請注意,這是一場戲! 作業,因此您可以手動啟動它,也可以使用Play提供的其他注釋! 例如@On或@Every。 有關更多信息,請參見“ 異步作業”文檔 。 當然,該代碼可在Github上獲得 。
現在開始游戲!
參考: RabbitMQ Play模塊! 來自JCG合作伙伴 Felipe Oliveira在Geeks的 框架 完全在 。
相關文章:
- Java Code Geeks Andygene Web原型
- 每個程序員都應該知道的事情
- Spring MVC開發–快速教程
- SmartGWT入門,提供出色的GWT界面
- GWT 2 Spring 3 JPA 2 Hibernate 3.5教程
翻譯自: https://www.javacodegeeks.com/2011/04/rabbitmq-module-play-framework.html