KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > queues > PushGeneratorImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: dream@objectweb.org
20  *
21  * Initial developer(s): Vivien Quema
22  * Contributor(s):
23  */

24
25 package queues;
26
27 import java.util.Collections JavaDoc;
28
29 import org.objectweb.dream.AbstractComponent;
30 import org.objectweb.dream.Push;
31 import org.objectweb.dream.control.activity.Util;
32 import org.objectweb.dream.control.activity.task.AbstractTask;
33 import org.objectweb.dream.message.ChunkAlreadyExistsException;
34 import org.objectweb.dream.message.Message;
35 import org.objectweb.dream.message.MessageType;
36 import org.objectweb.dream.message.MessageTypeImpl;
37 import org.objectweb.dream.message.manager.MessageManager;
38 import org.objectweb.dream.queue.SequenceNumberChunk;
39 import org.objectweb.fractal.api.Component;
40 import org.objectweb.fractal.api.NoSuchInterfaceException;
41 import org.objectweb.fractal.api.control.IllegalBindingException;
42 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
43 import org.objectweb.util.monolog.api.BasicLevel;
44
45 /**
46  * This component has a task that periodically pushes a message on its Push
47  * output.
48  */

49 public class PushGeneratorImpl extends AbstractComponent
50 {
51
52   Push outPushItf;
53   MessageManager messageManagerItf;
54   MessageType msgType;
55   int[] sns = {0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11,
56       12, 13, 14, 15, 2, 3, 4, 5, 6 };
57   protected static final Object JavaDoc LOCK = new Object JavaDoc();
58   int counter = 0;
59
60   /**
61    * Constructor.
62    */

63   public PushGeneratorImpl()
64   {
65     try
66     {
67       msgType = new MessageTypeImpl(HelloWorldChunk.DEFAULT_NAME,
68           HelloWorldChunk.TYPE, SequenceNumberChunk.DEFAULT_NAME,
69           SequenceNumberChunk.TYPE);
70     }
71     catch (ChunkAlreadyExistsException e)
72     {
73       // Cannot happen
74
}
75   }
76
77   // -------------------------------------------------------------------------
78
// Overriden methods
79
// -------------------------------------------------------------------------
80

81   /**
82    * @see org.objectweb.dream.AbstractComponent#beforeFirstStart(org.objectweb.fractal.api.Component)
83    */

84   protected void beforeFirstStart(Component componentItf)
85       throws IllegalLifeCycleException
86   {
87     try
88     {
89       Util.addTask(componentItf, new GeneratorTask("Push generator task1"),
90           Collections.EMPTY_MAP);
91       Util.addTask(componentItf, new GeneratorTask("Push generator task2"),
92           Collections.EMPTY_MAP);
93       logger.log(BasicLevel.DEBUG, "tasks added");
94     }
95     catch (Exception JavaDoc e)
96     {
97       throw new IllegalLifeCycleException("Can't add task");
98     }
99   }
100
101   // ---------------------------------------------------------------------------
102
// Implementation of BindingController interface
103
// ---------------------------------------------------------------------------
104

105   /**
106    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
107    * Object)
108    */

109   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
110       throws NoSuchInterfaceException, IllegalBindingException,
111       IllegalLifeCycleException
112   {
113     super.bindFc(clientItfName, serverItf);
114     if (clientItfName.equals(Push.OUT_PUSH_ITF_NAME))
115     {
116       outPushItf = (Push) serverItf;
117     }
118     else if (clientItfName.equals(MessageManager.ITF_NAME))
119     {
120       messageManagerItf = (MessageManager) serverItf;
121     }
122   }
123
124   /**
125    * @see org.objectweb.fractal.api.control.BindingController#listFc()
126    */

127   public String JavaDoc[] listFc()
128   {
129     return new String JavaDoc[]{Push.OUT_PUSH_ITF_NAME, MessageManager.ITF_NAME};
130   }
131
132   // ---------------------------------------------------------------------------
133
// The task.
134
// ---------------------------------------------------------------------------
135

136   private class GeneratorTask extends AbstractTask
137   {
138
139     private String JavaDoc message;
140
141     /**
142      * Constructor
143      */

144     public GeneratorTask(String JavaDoc message)
145     {
146       super(message);
147       this.message = message;
148     }
149
150     /**
151      * @see org.objectweb.dream.control.activity.task.Task#execute(Object)
152      */

153     public Object JavaDoc execute(Object JavaDoc hints) throws InterruptedException JavaDoc
154     {
155       try
156       {
157         Message msg = messageManagerItf.createMessage(msgType);
158         HelloWorldChunk chunk = (HelloWorldChunk) msg
159             .getChunk(HelloWorldChunk.DEFAULT_NAME);
160         SequenceNumberChunk snchunk = (SequenceNumberChunk) msg
161             .getChunk(SequenceNumberChunk.DEFAULT_NAME);
162         synchronized (LOCK)
163         {
164           if (counter < 20)
165           {
166             // The 20 first messages are used to test ordering...
167
chunk.setMessage("Message " + message);
168             snchunk.setSequenceNumber(sns[counter]);
169             counter++;
170             outPushItf.push(msg, null);
171           }
172           else
173           {
174             chunk.setMessage("Message " + message);
175             snchunk.setSequenceNumber(counter++);
176             outPushItf.push(msg, null);
177           }
178         }
179       }
180       catch (Exception JavaDoc e)
181       {
182         e.printStackTrace();
183         return STOP_EXECUTING;
184       }
185       Thread.sleep(1000);
186       return EXECUTE_AGAIN;
187     }
188   }
189 }
Popular Tags