Этот новый модуль позволяет вам получать и создавать сообщения на экземпляре RabbitMQ из вашей Play! Рамочное приложение.
Установка
|
1
|
play install rabbitmq |
конфигурация
|
01
02
03
04
05
06
07
08
09
10
|
module.rabbitmq=${play.path}/modules/rabbitmq-0.0.1rabbitmq.host=localhostrabbitmq.port=5672rabbitmq.userName=guestrabbitmq.password=guestrabbitmq.vhost=/rabbitmq.exchangeType=directrabbitmq.durable=truerabbitmq.autoAck=falserabbitmq.basicQos=true |
Определить сообщение, которое будет использоваться очередью (просто POJO)
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
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() */ @Override public String toString() { return "SampleMessage [field1=" + field1 + ", field2=" + field2 + "]"; }} |
Опубликовать сообщение
|
1
2
3
4
|
public static void publish(String q) { RabbitMQPublisher.publish("myQueue", new SampleMessage(q, q)); render(q); } |
Создание получателя сообщения
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
@OnApplicationStart(async=true)public class RabbitMQSampleConsumer extends RabbitMQConsumer { /** * Consume Message * * @see play.modules.rabbitmq.consumer.RabbitMQConsumer#consume(T) */ @Override protected 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() */ @Override protected 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 — еще один способ публикации сообщений в пакетном режиме
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
@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) */ @Override protected 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() */ @Override protected int batchSize() { return 2; } /** * Queue Name. * * @return the string * @see play.modules.rabbitmq.producer.RabbitMQFirehose#queueName() */ @Override protected String queueName() { return "myQueue"; }} |
* Обратите внимание, что это игра! задание, так что вы можете запустить его вручную или использовать другие аннотации от Play! как @On или @Every. Более подробная информация доступна в документации по асинхронным рабочим местам . Конечно, код доступен на Github .
Теперь иди играть!
Справка: Модуль RabbitMQ для игр! Каркас от нашего партнера JCG Фелипе Оливейры в Geeks Are Totally In .
Статьи по Теме: