KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > helloworld > PoolClientImpl


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

23
24 package helloworld;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.objectweb.dream.control.activity.Util;
30 import org.objectweb.dream.control.activity.task.Task;
31 import org.objectweb.dream.control.activity.task.TaskController;
32 import org.objectweb.dream.control.activity.task.thread.ThreadPoolController;
33 import org.objectweb.fractal.api.Component;
34 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
35 import org.objectweb.fractal.julia.control.lifecycle.ChainedIllegalLifeCycleException;
36
37 /**
38  * Client implementation, using thread pool tasks.
39  */

40 public class PoolClientImpl extends AbstractClientImpl
41 {
42
43   Task task = new ClientTask("Hello World task");
44
45   // -------------------------------------------------------------------------
46
// Overriden methods
47
// -------------------------------------------------------------------------
48

49   /**
50    * @see org.objectweb.dream.AbstractComponent#beforeFirstStart(org.objectweb.fractal.api.Component)
51    */

52   protected void beforeFirstStart(Component componentItf)
53       throws IllegalLifeCycleException
54   {
55     try
56     {
57       Map JavaDoc m = new HashMap JavaDoc();
58       m.put("thread", "pool");
59       m.put("threadPool.capacity", new Integer JavaDoc(2));
60       Util.addTask(componentItf, task, m);
61     }
62     catch (Exception JavaDoc e)
63     {
64       throw new IllegalLifeCycleException("Can't add task");
65     }
66   }
67
68   /**
69    * @see org.objectweb.dream.AbstractComponent#startFc()
70    */

71   public void startFc() throws IllegalLifeCycleException
72   {
73     super.startFc();
74     // add two threads in the thread pool.
75
try
76     {
77       TaskController tc = (TaskController) weaveableC
78           .getFcInterface("task-controller");
79       ThreadPoolController threadPoolController = (ThreadPoolController) tc
80           .getTaskControl(task);
81       threadPoolController.addThreads(2);
82     }
83     catch (Exception JavaDoc e)
84     {
85       throw new ChainedIllegalLifeCycleException(e, null,
86           "An error occurs while retreiving task control interface");
87     }
88   }
89 }
Popular Tags