---
title: "配置tomcat支持comet"
author: "tomEnjoy"
date: "2011-01-30T13:14:24Z"
tags: []
categories: ["企业架构"]
slug: "tomcat-comet"
draft: false
lastmod: "2011-02-22T23:26:56Z"
id: 899715
url: "https://tomenjoy.iteye.com/blog/899715"
---

使用 Java 实现 Comet 风格的 Web 应用 实现 Servlet 3.0 规范
<http://www.ibm.com/developerworks/cn/web/wa-cometjava/>
通过 Tomcat Advanced I/O 获得高性能的 Ajax
<http://www.ibm.com/developerworks/cn/web/wa-aj-hiperform/>

在参考上面两篇文章配置tomcat支持comet时遇到一些问题，现总结如下：
1.tomcat官方明确指明实现异步的IO需要APR或NIO的支持，我在这里是配置了APR。（IMPORTANT NOTE: Usage of these features requires using the APR or NIO HTTP connectors. The classic java.io HTTP connector and the AJP connectors do not support them.）
直接去http://tomcat.apache.org/tomcat-6.0-doc/apr.html下载tcnative-1.dll放在bin目录下即可。
2.需要明确在项目里引用catalina.jar包，该包在tomcat的lib目录下。
3.为避免tomcat重复加载catalina.jar,我们在server.xml里配置context如下:

```xml
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
    <Context path="/mis2011">
	<Loader delegate="true" />
	<Resource name="mis2011" auth="Container" type="javax.sql.DataSource"
		username="root"
		password=""
		driverClassName="com.mysql.jdbc.Driver"
		url="jdbc:mysql://localhost:3306/..."
		maxActive="30"
		maxIdle="5"
		maxWait="50"
		minEvictableIdleTimeMillis="4000"
		timeBetweenEvictionRunsMillis="5000"
	/>
    </Context>
</host>
```

3.按上面第一篇文章的介绍配置Servlet即可。