Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/hermes/broker/MessageTransaction.java


1   /*
2    * MessageTransaction.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  import java.util.LinkedList;
15  
16  /**
17   * <p>Provides a mechanism to collect several messages published in succession.
18   * The transaction may be commited or rolled back at any time.</p>
19   *
20   * <p>When commited, the messages are sent to any subscribed listeners (see {@link MessageListener}).</p>
21   *
22   * <p>When rolled back, all messages published during the transaction are thrown away and
23   * not sent to the listeners.</p>
24   *
25   * @author  Trevor Milne
26   *
27   */
28  
29  public class MessageTransaction
30  {
31    /** Contains the messages published to this batch. */
32    protected MessageBatch messages;
33  
34    /** Specifies whether the transaction is current active. */
35    protected boolean active;
36  
37  
38    /* Constructors. */
39  
40  
41    /**
42     * Constructs a <code>MessageTransaction</code>.
43     *
44     * @param    setConnection        The {@link MessageBrokerConnection} to publish through.
45     *
46     */
47  
48    protected MessageTransaction(MessageBrokerConnection setConnection)
49    {
50      // create batch
51      messages = new MessageBatch(setConnection);
52  
53      // initially inactive
54      active = false;
55    }
56  
57  
58    /* Transaction management. */
59  
60  
61    /**
62     * Starts a transaction. All messages published are handle within the transaction
63     * until either a commit or rollback is performed.
64     *
65     */
66  
67    public void start()
68    {
69      // start the batch
70      messages.start();
71  
72      // make the transaction active
73      active = true;
74    }
75  
76    /**
77     * Commits the transaction. All message published during the transaction will be
78     * sent to any listeners (see {@link MessageListener}).
79     *
80     */
81  
82    public void commit()
83    {
84      // inactivate the transaction
85      active = false;
86  
87      // send the batch
88      messages.send();
89    }
90  
91    /**
92     * Tells the transaction to rollback. All messages published during the transaction are
93     * thrown away and not sent to the listeners.
94     *
95     */
96  
97    public void rollback()
98    {
99      // clear the batch
100     messages.clear();
101 
102     // inactivate the transaction
103     active = false;
104   }
105 
106   /**
107    * Returns true if a transaction is currently in progress.
108    *
109    * @return                  True if the transaction is active, false otherwise.
110    *
111    */
112 
113   public boolean isActive()
114   {
115     return active;
116   }
117 
118 
119   /* Publishing. */
120 
121 
122   /**
123    * Publishes a message to the specific topic.
124    *
125    * @param    topic            The {@link MessageTopic} to publish the message to.
126    * @param    message            The {@link Message} to publish.
127    *
128    */
129 
130   public void publish(MessageTopic topic, Message message)
131   {
132     messages.publish(topic, message);
133   }
134 }
135