如何将你的activiti项目发布到tomcat下

本文介绍了如何将使用Eclipse插件创建的Activiti项目发布到Tomcat服务器。详细讲述了项目结构,包括各个文件的作用,如Servlet、JUnit测试、Spring配置、流程图等。还分享了数据库配置、打包成WAR文件、创建WEB-INF目录和web.xml文件的过程,以及最终部署到Tomcat的步骤。通过访问特定URL可以查看和管理项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里,我所创建的activiti项目是利用Eclipse下的插件完成的,其目录结构默认采取maven的结构,项目如下图所示:

注:其中WEB-INF的文件夹是我自己加上去的,原项目没有。

这里我先解释一下每一个文件的内容及作用(由上往下):

Myworkslt.java是我自己编写的一个Servlet,为了将项目发布到网上而构建的。

package com.demo.activiti;

import javax.servlet.*;
import javax.servlet.http.*;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class Myworkslt extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	 
	  public void init(ServletConfig config) throws ServletException {
		  super.init(config);
	  }


	  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	   PrintWriter out = response.getWriter();
	   ProcessEngine processEngine = null;
	   processEngine = ProcessEngines.getDefaultProcessEngine();
	   RepositoryService repositoryService = processEngine.getRepositoryService();
	   RuntimeService runtimeService = processEngine.getRuntimeService();
	 
	   Deployment deployment = repositoryService.createDeployment()
	    .addClasspathResource("com/demo/activiti/MyProcess.bpmn20.xml")
	   .deploy();
	   
	   String name = "第一个任务";
		int level=1;
		Map<String,Object> startVar = new HashMap<String, Object>();
		startVar.put("name", name);
		startVar.put("level", level);
	   ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("MyProcess",startVar);
	   out.println("Started Process instance id " +processInstance.getProcessInstanceId()); 
	  }
	 
	  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
		  this.doGet(request,response);
	  }
}


TestMyProcess.java顾名思义,是为了测试我们的项目而编写的JUnit测试文件,项目发布时不会用到。

activiti-context.xml为spring MVC配置文件,项目发布时需要用到。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<!-- 流程业务服务类 -->
  <bean id="myWorkService" class="com.demo.activiti.service.MyWorkService" />
  
  <!-- 数据库配置文件 -->
  <context:property-placeholder location="classpath*:*.properties" />

  <!-- 数据源 -->
  <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <property name="driverClass" value="${jdbc.driver}" />
	<property name="url" value="${jdbc.url}" />
	<property name="username" value="${jdbc.username}" />
	<property name="password" value="${jdbc.password}" />
  </bean>
  <!-- 事务管理器 -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
  
  <!-- 流程引擎配置 -->
  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="true" />
    <property name="jobExecutorActivate" value="false" />
  </bean>
  <!-- 流程引擎 -->
  <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
    <property name="processEngineConfiguration" ref="processEngineConfiguration" />
  </bean>
  
  <!-- 工作流内部服务类 -->
  <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
  <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
  <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
  <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
  <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />

</beans>


MyWork.java相当于一个javabean,包含了任务所需的一些属性。

MyWorkService.java主要包含了流程所需的一些服务、方法。

MyProcess.activiti为创建的activiti流程图,下面的.xml和.png都是根据这个文件而自动生成的,如果想要修改流程,一定要先修改这个文件,然后保存一下,配置文件和图片才会自动更新。

项目流程如图:


mysql.properties为数据库配置文件,内容如下:

db=mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/activiti?autoReconnect=true
jdbc.username=activiti
jdbc.password=activiti

这里我所用到的数据库为mysql的,数据库名及用户名和密码都为activiti。

文件全部介绍完了,下一步就是如何发布了。

首先,进入命令行,利用maven将项目打包称war文件:mvn war:war;当然,由于缺失web-xml文件,命令结果会失败,但是生成了我们所需的lib和classes文件。

将这两个文件夹拷出,放入到新建的一个WEB-INF文件夹里面,并在里面新建一个web-xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                                                http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
   	<display-name>Vaadin Web Application</display-name>

	<context-param>
		<description>Vaadin production mode</description>
		<param-name>productionMode</param-name>
		<param-value>true</param-value>
	</context-param>

  <!-- To load the Spring context -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- To allow session-scoped beans in Spring -->
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener> 
    
    <servlet>
         <servlet-name>hello</servlet-name>
         <servlet-class>com.demo.activiti.Myworkslt</servlet-class>
    </servlet>

    <servlet-mapping>
         <servlet-name>hello</servlet-name>
         <url-pattern>/hello</url-pattern>
    </servlet-mapping>
        
</web-app>

这里将我们的服务名起名为hello,URL为/hello。前面一段代码是利用spring的监听器,去读取我们的spring配置文件。将前面介绍的配置文件拷贝到与web-xml文件同一目录下,并改名为applicationContext.xml。

与WEB-INF同级目录下建立META-INF文件夹,包含MANIFEST.MF文件。完成后目录结构如下:

一起放入到activiti-demo文件夹下后,拷贝到tomcatwebapp目录下,启动tomcat,访问http://localhost:8080/activiti-demo/hello就可以访问到我们的发布的项目了。也可以通过http://localhost:8080/activiti-explorer来管理我们的项目。

个人水平有限,如果有什么不好的地方,请您指出,多多包涵。

参考资料:http://www.mastertheboss.com/component/content/article/302-activiti-tutorial.html?start=2




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值