Source code: com/aendvari/hermes/broker/MessageBatch.java
1 /*
2 * MessageBatch.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.hermes.broker;
11
12 import java.util.Iterator;
13 import java.util.ArrayList;
14
15 /**
16 * <p>Provides a mechanism to collect several messages published in succession.</p>
17 *
18 * <p>The messages may then sent to any subscribed listeners (see {@link MessageListener}).
19 * If the batch is not sent, the messages are thrown away and not sent to the listeners.</p>
20 *
21 * @author Trevor Milne
22 *
23 */
24
25 public class MessageBatch
26 {
27 /** The {@link MessageBrokerConnection} to send the messages through. */
28 protected MessageBrokerConnection brokerConnection;
29
30 /** Contains the topics published to this batch. */
31 protected ArrayList topics;
32
33 /** Contains the messages published to this batch. */
34 protected ArrayList messages;
35
36 /**
37 * Contains the current unique identifier for the batch. The identifier changes everytime a new
38 * batch is started (see {@link #start start} or sent (see {@link #send send}).
39 *
40 */
41 protected long uniqueIdentifier;
42
43
44 /* Constructors. */
45
46
47 /**
48 * Constructs a <code>MessageBatch</code>.
49 *
50 * @param setConnection The {@link MessageBrokerConnection} to publish through.
51 *
52 */
53
54 public MessageBatch(MessageBrokerConnection setConnection)
55 {
56 brokerConnection = setConnection;
57
58 topics = new ArrayList();
59 messages = new ArrayList();
60
61 // initially, start a batch
62 start();
63 }
64
65
66 /* Accessors. */
67
68
69 /**
70 * Returns the unique identifier for this batch.
71 *
72 * @return The unique identifier for this batch.
73 *
74 */
75
76 public long getIdentifier()
77 {
78 return uniqueIdentifier;
79 }
80
81
82 /* Batch management. */
83
84
85 /**
86 * Prepares this <code>MessageBatch</code> for publishing.
87 *
88 */
89
90 public void start()
91 {
92 // set the unique identifier
93 uniqueIdentifier = System.currentTimeMillis();
94
95 // clear the message batch
96 topics.clear();
97 messages.clear();
98 }
99
100 /**
101 * Send the batch. All message published to this batch will be sent to any listeners
102 * (see {@link MessageListener}).
103 *
104 */
105
106 public void send()
107 {
108 // send each message in batch
109 Iterator topicIterator = topics.iterator();
110 Iterator messageIterator = messages.iterator();
111
112 while (messageIterator.hasNext())
113 {
114 // get topic
115 MessageTopic topic = (MessageTopic)topicIterator.next();
116 Message message = (Message)messageIterator.next();
117
118 // publish message to broker
119 brokerConnection.publish(topic, message);
120 }
121 }
122
123 /**
124 * Removes all messages currently in the batch.
125 *
126 */
127
128 public void clear()
129 {
130 // clears the batch
131 start();
132 }
133
134
135 /* Publishing. */
136
137
138 /**
139 * Publishes a message to the specific topic.
140 *
141 * @param topic The {@link MessageTopic} to publish the message to.
142 * @param message The {@link Message} to publish.
143 *
144 */
145
146 public void publish(MessageTopic topic, Message message)
147 {
148 // add topic/message to batch
149 topics.add(topic);
150 messages.add(message);
151 }
152 }
153