package com.ailk; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.util.thread.QueuedThreadPool; public class JettyServer { public static void main(String[] args) { JettyServer jettyServer = new JettyServer(); jettyServer.doRun(); } public void doRun() { Server server = new Server(); SelectChannelConnector connector = new SelectChannelConnector(); connector.setPort(9990); connector.setMaxIdleTime(30000); connector.setRequestHeaderSize(8192); QueuedThreadPool threadPool = new QueuedThreadPool(50); threadPool.setName("embed-jetty-http"); connector.setThreadPool(threadPool); server.setConnectors(new Connector[] { connector }); server.setHandler(JettyServerHandlerFactory.getHandle()); try { server.start(); server.join(); } catch (Exception e) { e.printStackTrace(); } } }
package com.ailk; //import org.apache.axis.transport.http.AdminServlet; //import org.apache.axis.transport.http.AxisServlet; import org.codehaus.xfire.transport.http.XFireConfigurableServlet; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.server.handler.IPAccessHandler; import org.eclipse.jetty.server.session.SessionHandler; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; public final class JettyServerHandlerFactory { public static Handler getHandle() { IPAccessHandler ipAccessHandler = getFireWallHandler(); ipAccessHandler.setHandler(getServletHandler()); HandlerCollection handlers = new HandlerCollection(); handlers.setHandlers(new Handler[] { ipAccessHandler, new DefaultHandler() }); return handlers; } private static IPAccessHandler getFireWallHandler() { IPAccessHandler ipHandler = new IPAccessHandler(); return ipHandler; } private static Handler getServletHandler() { ServletContextHandler root = new ServletContextHandler( ServletContextHandler.SESSIONS); root.setSessionHandler(new SessionHandler()); root.setContextPath("/"); // root.setResourceBase("./target/classes/axis/"); // /axis/WEB-INF/server-config.wsdd root.setResourceBase("./target/classes/META-INF/xfire/services.xml"); // XFireServlet ServletHolder holder = new ServletHolder(new XFireConfigurableServlet()); root.addServlet(holder, "/servlet/XFireServlet"); root.addServlet(holder, "/services/*"); return root; } }
package com.ailk; public interface MainService { public String getUserInfo(String username); }package com.ailk; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MainServiceImpl implements MainService { private static final Logger log = LoggerFactory .getLogger(MainServiceImpl.class); @Override public String getUserInfo(String username) { log.info("Req:{}", username); String rsp = String.valueOf(Math.random()); log.info("Rsp:{}", rsp); return rsp; } }
src/main/resources下META-INF/xfire/services.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans> <service xmlns="http://xfire.codehaus.org/config/1.0"> <name>jettyDemo</name> <namespace>http://www.ailk.com/jettyDemo</namespace> <serviceClass>com.ailk.MainService</serviceClass> <implementationClass>com.ailk.MainServiceImpl</implementationClass> </service> </beans>
pom.xml<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>study-jetty</groupId> <artifactId>study-jetty</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <sourceDirectory>src/main/java</sourceDirectory> <testSourceDirectory>src/test/java</testSourceDirectory> <testOutputDirectory>target/test/classes</testOutputDirectory> <outputDirectory>target/classes</outputDirectory> <testResources> <testResource> <directory>src/test/resources</directory> </testResource> </testResources> </build> <dependencies> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>8.1.14.v20131031</version> </dependency> <dependency> <groupId>org.codehaus.xfire</groupId> <artifactId>xfire-all</artifactId> <version>1.2.6</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>8.1.14.v20131031</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.11</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.0.11</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.6</version> </dependency> </dependencies> </project>
启动server后,浏览器访问:http://127.0.0.1:9990/services/jettyDemo?wsdl
说明:
1.需使用jdk1.6
2.com.ailk.JettyServerHandlerFactory.getServletHandler()中使用XFireConfigurableServlet
可以参照web工程中web.xml的配置