在项目启动的时候需要启动相关的activiti服务,在activiti通过使用servletcontextlistener实现在项目启动相关的服务。
package org.activiti.rest.servlet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.impl.util.LogUtil;
/**
* @author Tijs Rademakers
*/
public class ActivitiServletContextListener implements ServletContextListener {
//Initialise logging
static {
LogUtil.readJavaUtilLoggingConfigFromClasspath();
}
protected static final Logger LOGGER = Logger.getLogger(ActivitiServletContextListener.class.getName());
public void contextInitialized(ServletContextEvent event) {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
if (processEngine == null) {
LOGGER.log(Level.SEVERE,"Could not start the Activiti Engine");
}
}
public void contextDestroyed(ServletContextEvent event) {
ProcessEngines.destroy();
}
}
web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Activiti REST</display-name>
<listener>
<listener-class>org.activiti.rest.servlet.ActivitiServletContextListener</listener-class>
</listener>
<!-- Restlet adapter -->
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
<init-param>
<!-- Application class name -->
<param-name>org.restlet.application</param-name>
<param-value>org.activiti.rest.application.ActivitiRestApplication</param-value>
</init-param>
</servlet>
<!-- Catch all requests -->
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>