0
点赞
收藏
分享

微信扫一扫

JFinal整合spring的websocket


在使用JFinal整合spring使用spring的websocket的时候,遇到了很多问题,下面介绍整合的全过程和要注意的点。(整个项目使用maven进行搭建,服务器用的是eclipse自带的jetty)

1.整个pom.xml文件的代码如下:

需要注意的是,jetty的版本要是9.+,这个在spring websocket的官方文档中是有说明的。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.giant.app.gbot.chat</groupId>
<artifactId>app-gbot-chat</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>app-gbot-chat Maven Webapp</name>
<url>http://maven.apache.org</url>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>1.6</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-websocket -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-messaging -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>


<!-- cos -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>cos</artifactId>
<version>26Dec2008</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>de.ruedigermoeller</groupId>
<artifactId>fst</artifactId>
<version>2.29</version>
</dependency>

<!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId>
<version>5.1.20</version> </dependency> -->

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.29</version>
</dependency>

<dependency>
<groupId>it.sauronsoftware.cron4j</groupId>
<artifactId>cron4j</artifactId>
<version>2.2.5</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>


</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.3.v20140905</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<contextXml>${project.basedir}/src/test/resources/jetty-context.xml</contextXml>
<webApp>
<contextPath>/</contextPath>
<defaultsDescriptor>jettyCustom.xml</defaultsDescriptor>
</webApp>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>


</plugins>
</build>

<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>

2.然后配置web.xml,因为这个项目的controller层是JFinal的,所以JFinal需要一个顶级的过滤器 ​​/*​​ ,然后二级过滤是spring的 DispatcherServlet,配置如下:

<filter>
<filter-name>jfinal</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
<init-param>
<param-name>configClass</param-name>
<param-value>com.giant.gbot.chat.AppConfig</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>chat</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

</servlet>
<servlet-mapping>
<servlet-name>chat</servlet-name>
<url-pattern>/chat</url-pattern>
</servlet-mapping>

3.经过上面的配置之后,发现websocket请求的url还是被JFinal过滤掉了,所以需要在JFinal的配置代码APPConfig中,覆盖configHandler,添加不过滤的url,代码如下:

@Override
public void configHandler(Handlers me) {
// TODO Auto-generated method stub
me.add(new UrlSkipHandler("/chat", false));
}

经过上面的步骤,已经可以使用websocket了。

4.如果想要在websocket或者JFinal的controller中使用spring的IOC等特性,还需要在JFinal中添加spring插件。代码如下:

@Override
public void configPlugin(Plugins me) {

// 加载数据库配置文件
loadPropertyFile("database.properties");
// 从配置文件中获取数据库配置项
String driverClass = getProperty("driverClass");
String jdbcUrl = getProperty("url");
String username = getProperty("username");
String password = getProperty("password");

C3p0Plugin cp;
cp = new C3p0Plugin(jdbcUrl, username, password);
cp.setDriverClass(driverClass);
cp.setInitialPoolSize(3).setMaxIdleTime(180);
me.add(cp);
// 初始化activerecord
ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);

arp.addMapping("online_status", "id", OnlineStatus.class);

// 开发阶段,显示sql
arp.setShowSql(true);

arp.setDialect(new MysqlDialect());

// 配置属性名(字段名)大小写不敏感容器工厂
arp.setContainerFactory(new CaseInsensitiveContainerFactory());

arp.addMapping("user", "userId", User.class);

me.add(arp);

File file = new File("/" + PathKit.getWebRootPath() + "/WEB-INF/applicationContext.xml");
if (file.exists()) {
me.add(new SpringPlugin(SpringKit.initContext("/" + PathKit.getWebRootPath() + "/WEB-INF/applicationContext.xml")));
} else {
me.add(new SpringPlugin(SpringKit.initContext(PathKit.getWebRootPath() + "/WEB-INF/applicationContext.xml")));
}


logger.info("加载配置文件");
}


举报

相关推荐

0 条评论