This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
/
GenericPoolableRemoteDeliveryFactory.java
80 lines (71 loc) · 2.43 KB
/
GenericPoolableRemoteDeliveryFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package org.masukomi.aspirin.core;
import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.ObjectPool;
/**
* <p>This object handles the RemoteDelivery thread objects in the ObjectPool.
* </p>
*
* @version $Id$
*
*/
public class GenericPoolableRemoteDeliveryFactory extends BasePoolableObjectFactory {
/**
* This is the ThreadGroup of RemoteDelivery objects. On shutdown it is
* easier to close all RemoteDelivery threads with usage of this group.
*/
private ThreadGroup remoteDeliveryThreadGroup = null;
private ObjectPool myParentPool = null;
/**
* This is the counter of created RemoteDelivery thread objects.
*/
private Integer rdCount = 0;
/**
* <p>Initialization of this Factory. Prerequisite of right working.</p>
*
* @param deliveryThreadGroup The threadgroup which contains the
* RemoteDelivery threads.
* @param pool The pool which use this factory to create and handle objects.
*/
public void init(ThreadGroup deliveryThreadGroup, ObjectPool pool) {
remoteDeliveryThreadGroup = deliveryThreadGroup;
myParentPool = pool;
}
@Override
public Object makeObject() throws Exception {
if( myParentPool == null )
throw new RuntimeException("Please set the parent pool for right working.");
RemoteDelivery rd = new RemoteDelivery(remoteDeliveryThreadGroup);
synchronized (rdCount) {
rdCount++;
rd.setName(RemoteDelivery.class.getSimpleName()+"-"+rdCount);
}
rd.setParentPool(myParentPool);
Aspirin.getConfiguration().getLog().trace(getClass().getSimpleName()+".makeObject(): New RemoteDelivery object created: "+rd.getName());
/*
* This will be started after first borrow of this object, because the
* first item to process will be set after first borrow too.
*/
// rd.start();
Aspirin.getConfiguration().addListener(rd);
return rd;
}
@Override
public void destroyObject(Object obj) throws Exception {
if( obj instanceof RemoteDelivery )
{
RemoteDelivery rd = (RemoteDelivery)obj;
Aspirin.getConfiguration().getLog().trace(getClass().getSimpleName()+".destroyObject(): destroy thread "+rd.getName());
rd.shutdown();
Aspirin.getConfiguration().removeListener(rd);
}
}
@Override
public boolean validateObject(Object obj) {
if( obj instanceof RemoteDelivery )
{
RemoteDelivery rd = (RemoteDelivery)obj;
return rd.isAlive();
}
return false;
}
}