本文源自:https://github.com/springside/springside4/wiki/Jetty
jetty 是servlet容器,是一个嵌入式的servlet容器:
1,可以在eclipse中,通过java代码,直接启动此容器,例如:QuickstartServer.java、ShowcaseService.java
package org.springside.examples.quickstart.functional;
import org.eclipse.jetty.server.Server;
import org.springside.modules.test.jetty.JettyFactory;
/**
* 使用Jetty运行调试Web应用, 在Console输入回车快速重新加载应用.
*
* @author calvin
*/
public class QuickStartServer {
public static final int PORT = 8080;
public static final String CONTEXT = "/quickstart";
public static final String BASE_URL = "http://localhost:8080/quickstart";
public static final String[] TLD_JAR_NAMES = new String[] { "sitemesh", "spring-webmvc", "shiro-web",
"springside-core" };
public static void main(String[] args) throws Exception {
// 设定Spring的profile
System.setProperty("spring.profiles.active", "development");
// 启动Jetty
Server server = JettyFactory.createServerInSource(PORT, CONTEXT);
JettyFactory.setTldJarNames(server, TLD_JAR_NAMES);
try {
server.start();
System.out.println("Server running at " + BASE_URL);
System.out.println("Hit Enter to reload the application");
// 等待用户输入回车重载应用.
while (true) {
char c = (char) System.in.read();
if (c == '\n') {
JettyFactory.reloadContext(server);
}
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
}
2,直接在用例中 启动Jetty,而不是先将应用打包部署到容器。在各个项目的BaseFunctionalTest中有演示。
package org.springside.examples.quickstart.functional;
import java.net.URL;
import java.sql.Driver;
import org.eclipse.jetty.server.Server;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springside.modules.test.data.DataFixtures;
import org.springside.modules.test.jetty.JettyFactory;
import org.springside.modules.test.selenium.Selenium2;
import org.springside.modules.utils.PropertiesLoader;
/**
* 功能测试基类.
*
* 在整个测试期间启动一次Jetty Server和 Selenium,在JVM退出时关闭两者。
* 在每个TestCase Class执行前重新载入默认数据.
*
* @author calvin
*/
public class BaseFunctionalTestCase {
protected static String baseUrl;
protected static Server jettyServer;
protected static SimpleDriverDataSource dataSource;
protected static Selenium2 s;
protected static PropertiesLoader propertiesLoader = new PropertiesLoader("classpath:/application.properties",
"classpath:/application.functional.properties", "classpath:/application.functional-local.properties");
private static Logger logger = LoggerFactory.getLogger(BaseFunctionalTestCase.class);
@BeforeClass
public static void beforeClass() throws Exception {
baseUrl = propertiesLoader.getProperty("baseUrl", QuickStartServer.BASE_URL);
//如果是目标地址是localhost,则启动嵌入式jetty。如果指向远程地址,则不需要启动Jetty.
Boolean isEmbedded = new URL(baseUrl).getHost().equals("localhost");
if (isEmbedded) {
startJettyOnce();
}
buildDataSourceOnce();
reloadSampleData();
}
/**
* 启动Jetty服务器, 仅启动一次.
*/
protected static void startJettyOnce() throws Exception {
if (jettyServer == null) {
//设定Spring的profile
System.setProperty("spring.profiles.active", "functional");
jettyServer = JettyFactory.createServerInSource(new URL(baseUrl).getPort(), QuickStartServer.CONTEXT);
JettyFactory.setTldJarNames(jettyServer, QuickStartServer.TLD_JAR_NAMES);
jettyServer.start();
logger.info("Jetty Server started");
}
}
/**
* 构造数据源,仅构造一次.
* 连接参数从配置文件中读取,可指向本地的开发环境,也可以指向远程的测试服务器。
*/
protected static void buildDataSourceOnce() throws ClassNotFoundException {
if (dataSource == null) {
dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass((Class<? extends Driver>) Class.forName(propertiesLoader
.getProperty("jdbc.driver")));
dataSource.setUrl(propertiesLoader.getProperty("jdbc.url"));
dataSource.setUsername(propertiesLoader.getProperty("jdbc.username"));
dataSource.setPassword(propertiesLoader.getProperty("jdbc.password"));
}
}
/**
* 载入测试数据.
*/
protected static void reloadSampleData() throws Exception {
DataFixtures.executeScript(dataSource, "classpath:data/cleanup-data.sql", "classpath:data/import-data.sql");
}
}
3,其他方面
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com