Last active
April 20, 2016 12:24
-
-
Save geosmart/ee8ca82b03dfa10a2bdb58767060fa6f to your computer and use it in GitHub Desktop.
GATE Pipeline池资源管理( 在Web多线程环境下建立SerialAnalyserController池分配资源)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gate="http://gate.ac.uk/ns/spring" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans.xsd | |
http://gate.ac.uk/ns/spring http://gate.ac.uk/ns/spring.xsd"> | |
<description>GATE 池资源</description> | |
<!--Pipeline Pool Config--> | |
<bean id="pipelinePool" class="org.apache.commons.pool2.impl.GenericObjectPool"> | |
<constructor-arg index="0" ref="pipelinePooledObjectFactory"></constructor-arg> | |
<constructor-arg index="1" ref="genericObjectPoolConfig"></constructor-arg> | |
</bean> | |
<!-- Generic PoolConfig --> | |
<bean id="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig"> | |
<property name="maxIdle" value="5"></property> | |
<property name="maxTotal" value="100"></property> | |
<property name="maxWaitMillis" value="1000"></property> | |
</bean> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gto.uadb.test; | |
import gate.Corpus; | |
import gate.common.service.IGATECommonService; | |
import gate.creole.ResourceInstantiationException; | |
import gate.creole.SerialAnalyserController; | |
import gto.uadb.service.IAddressProcessPipeline; | |
import gto.uadb.test.util.AnnotationTestUtil; | |
import java.net.MalformedURLException; | |
import javax.annotation.Resource; | |
import org.apache.commons.pool2.impl.GenericObjectPool; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
/** | |
* GATE Pool测试示例 | |
* | |
* @author geosmart | |
* | |
*/ | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(locations = {"classpath:app.etl.extract.cfg.xml"}) | |
public class GATEPoolTest { | |
@Resource(name = "pipelinePool") | |
GenericObjectPool<SerialAnalyserController> pipelinePool; | |
@Autowired | |
IGATECommonService gateCommonService; | |
@Resource(name = "extractPipelinService") | |
IAddressProcessPipeline pipelineService; | |
String currentPlugin = "gto.uadb.etl.extract"; | |
String gateDir = System.getProperty("user.dir") + "//src//test//resources//gate"; | |
private static boolean initialized = false; | |
@Before | |
public void setup() { | |
System.out.println("---setup"); | |
if (!initialized) { | |
// 初始化GATE环境 | |
gateCommonService.setGateDir(gateDir); | |
gateCommonService.initGate(); | |
gateCommonService.registerGatePlugins(new String[] {"ANNIE", currentPlugin}); | |
// 初始化Pipeline | |
pipelineService.initPipeline(); | |
initialized = true; | |
} | |
} | |
/** | |
* JAPE和Gazetteer测试 | |
*/ | |
@Test | |
public void testAnnotation() throws ResourceInstantiationException, MalformedURLException { | |
Corpus sourceCorpus = AnnotationTestUtil.getCorpus(); | |
loadPipeline(sourceCorpus); | |
} | |
/** | |
* 装载Pipeline管道 | |
* | |
* @param corpus 语料库 | |
*/ | |
private Corpus loadPipeline(Corpus corpus) { | |
try { | |
SerialAnalyserController pipeline = pipelinePool.borrowObject(); | |
pipeline.setCorpus(corpus); | |
pipeline.execute(); | |
pipelinePool.returnObject(pipeline); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return corpus; | |
} | |
@After | |
public void teardown() { | |
System.out.println("---teardown"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gto.uadb.service.impl; | |
import gate.Gate; | |
import gate.creole.SerialAnalyserController; | |
import gate.util.persistence.PersistenceManager; | |
import java.io.File; | |
import org.apache.commons.pool2.BasePooledObjectFactory; | |
import org.apache.commons.pool2.PooledObject; | |
import org.apache.commons.pool2.impl.DefaultPooledObject; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
/** | |
* 面向Web多线程的SerialAnalyserController池分配 PooledObjectFactory | |
* | |
* @author geosmart | |
* | |
*/ | |
public class PipelinePooledObjectFactory extends BasePooledObjectFactory<SerialAnalyserController> { | |
private static Logger log = LoggerFactory.getLogger(PipelinePooledObjectFactory.class); | |
String gappFilePath; | |
// borrow: | |
@Override | |
public void passivateObject(PooledObject<SerialAnalyserController> arg0) throws Exception { | |
cleanupProcessor(arg0.getObject()); | |
} | |
@Override | |
public void activateObject(PooledObject<SerialAnalyserController> arg0) throws Exception { | |
cleanupProcessor(arg0.getObject()); | |
} | |
@Override | |
public void destroyObject(PooledObject<SerialAnalyserController> arg0) throws Exception { | |
System.out.println("how to destry?"); | |
} | |
@Override | |
public boolean validateObject(PooledObject<SerialAnalyserController> arg0) { | |
return true; | |
} | |
@Override | |
public SerialAnalyserController create() throws Exception { | |
File file = new File(Gate.getGateHome().getPath() + gappFilePath); | |
SerialAnalyserController processsor = (SerialAnalyserController) PersistenceManager.loadObjectFromFile(file); | |
return processsor; | |
} | |
private void cleanupProcessor(SerialAnalyserController processsor) { | |
processsor.cleanup(); | |
} | |
@Override | |
public PooledObject<SerialAnalyserController> wrap(SerialAnalyserController obj) { | |
return new DefaultPooledObject<SerialAnalyserController>(obj); | |
} | |
public String getGappFilePath() { | |
return gappFilePath; | |
} | |
public void setGappFilePath(String gappFilePath) { | |
this.gappFilePath = gappFilePath; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- pom部分dependency --> | |
<dependencies> | |
<dependency> | |
<groupId>commons-pool</groupId> | |
<artifactId>commons-pool</artifactId> | |
<version>1.5.4</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-pool2</artifactId> | |
<version>2.4.2</version> | |
</dependency> | |
</dependencies> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment