KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > queues > PullGeneratorImpl


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.Pull;
31 import org.objectweb.dream.control.activity.Util;
32 import org.objectweb.dream.control.activity.task.AbstractTask;
33 import org.objectweb.dream.control.activity.task.Task;
34 import org.objectweb.dream.message.Message;
35 import org.objectweb.dream.message.manager.MessageManager;
36 import org.objectweb.dream.queue.SequenceNumberChunk;
37 import org.objectweb.fractal.api.Component;
38 import org.objectweb.fractal.api.NoSuchInterfaceException;
39 import org.objectweb.fractal.api.control.IllegalBindingException;
40 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
41 import org.objectweb.util.monolog.api.BasicLevel;
42
43 /**
44  * This component has a task that periodically pulls a message on its Pull
45  * input.
46  */

47 public class PullGeneratorImpl extends AbstractComponent
48 {
49
50   Pull inPullItf;
51   MessageManager messageManagerItf;
52
53   Task tasks;
54
55   // -------------------------------------------------------------------------
56
// Overriden methods
57
// -------------------------------------------------------------------------
58

59   /**
60    * @see org.objectweb.dream.AbstractComponent#beforeFirstStart(org.objectweb.fractal.api.Component)
61    */

62   protected void beforeFirstStart(Component componentItf)
63       throws IllegalLifeCycleException
64   {
65     try
66     {
67       Util.addTask(componentItf, new GeneratorTask("Pull generator task1"),
68           Collections.EMPTY_MAP);
69       Util.addTask(componentItf, new GeneratorTask("Pull generator task2"),
70           Collections.EMPTY_MAP);
71       logger.log(BasicLevel.DEBUG, "tasks added");
72     }
73     catch (Exception JavaDoc e)
74     {
75       throw new IllegalLifeCycleException("Can't add task");
76     }
77   }
78
79   // ---------------------------------------------------------------------------
80
// Implementation of BindingController interface
81
// ---------------------------------------------------------------------------
82

83   /**
84    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
85    * Object)
86    */

87   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
88       throws NoSuchInterfaceException, IllegalBindingException,
89       IllegalLifeCycleException
90   {
91     super.bindFc(clientItfName, serverItf);
92     if (clientItfName.equals(Pull.IN_PULL_ITF_NAME))
93     {
94       inPullItf = (Pull) serverItf;
95     }
96     else if (clientItfName.equals(MessageManager.ITF_NAME))
97     {
98       messageManagerItf = (MessageManager) serverItf;
99     }
100   }
101
102   /**
103    * @see org.objectweb.fractal.api.control.BindingController#listFc()
104    */

105   public String JavaDoc[] listFc()
106   {
107     return new String JavaDoc[]{Pull.IN_PULL_ITF_NAME, MessageManager.ITF_NAME};
108   }
109
110   // ---------------------------------------------------------------------------
111
// The task.
112
// ---------------------------------------------------------------------------
113

114   private class GeneratorTask extends AbstractTask
115   {
116
117     /**
118      * Constructor
119      */

120     public GeneratorTask(String JavaDoc message)
121     {
122       super(message);
123     }
124
125     /**
126      * @see Task#execute(Object)
127      */

128     public Object JavaDoc execute(Object JavaDoc hints) throws InterruptedException JavaDoc
129     {
130       try
131       {
132         Message msg = inPullItf.pull(null);
133         if (msg == null)
134         {
135           logger.log(BasicLevel.INFO, "Pulled null message");
136         }
137         else
138         {
139           HelloWorldChunk chunk = (HelloWorldChunk) msg
140               .getChunk(HelloWorldChunk.DEFAULT_NAME);
141           SequenceNumberChunk snchunk = (SequenceNumberChunk) msg
142               .getChunk(SequenceNumberChunk.DEFAULT_NAME);
143           logger.log(BasicLevel.INFO, "Sequence number = "
144               + snchunk.getSequenceNumber() + " msg = " + chunk.getMessage());
145           messageManagerItf.deleteMessage(msg);
146         }
147       }
148       catch (Exception JavaDoc e)
149       {
150         e.printStackTrace();
151         return STOP_EXECUTING;
152       }
153       Thread.sleep(1000);
154       return EXECUTE_AGAIN;
155     }
156   }
157 }
Popular Tags