Results 1 to 7 of 7

Thread: Jetty embedded in spring

  1. #1

    Default Jetty embedded in spring

    Hello Folks!!.

    I'm trying embed Jetty in Spring. This is my folder structure in the server side, this is a example that somebody has posted in this forum, but I don't remember the link, sorry by not to mention to Author:

    conf
    |
    |--WEB-INF
    | |
    | |--lib
    | | |
    | | |-- commons-logging.jar
    | | |-- jetty-6.1.3.jar
    | | |-- jetty-util-6.1.3.jar
    | | |-- servlet-api-2.5-6.1.3.jar
    | | |-- spring.jar
    | |
    | |-- applicationContext.xml
    | |-- remoting-servlet.xml
    | |-- web.xml
    |
    |-- jetty.xml

    The project has been developed with Eclipse.

    This my code snippets:

    HelloWorld.java:

    Code:
    public interface HelloWorld {
    	public String sayhello();
    }
    HelloWorldImpl.java

    Code:
    public class HelloWorldImpl implements HelloWorld {
    
    	public String sayhello() {
    		return "Hello";
    	}
    
    }
    TestServiceExporter.java

    Code:
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    public class TestServiceExporter
    {
    	public static void main( String[] args ){
    		FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext("jetty.xml");
    	}
    }
    applicationContext.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    	<!--
    	helloWorld
    	-->
    	<bean id="helloWorld" class="HelloWorldImpl">
    	</bean>
    </beans>
    remotingServlet.xml

    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <!--
    - Dispatcher servlet for HTTP remoting via Hessian, Burlap, and Spring's
    - HTTP invoker (see remoting-servlet.xml for the controllers).
    -->
    <beans>
    
    	<!-- HTTP invoker exporter for the JPetStore OrderService -->
    	<!-- Spring's HTTP invoker uses Java serialization via HTTP -->
    	<!--
    
    	ModeState
    
    	-->
    	 
    	<bean id="remoteMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="mappings">
    			<props>
    				<prop key="/helloWorld-httpinvoker">helloWorld-httpinvoker</prop>
    			</props>
    		</property>
    	</bean>
    	
    	<bean id="helloWorld-httpinvoker" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
    		<property name="service"><ref bean="helloWorld"/></property>
    		<property name="serviceInterface"><value>HelloWorld</value></property>
    	</bean>
    
    </beans>
    web.xml

    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    
    <web-app
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    
    <!--
    - Location of the XML file that defines the root application context.
    - Applied by ContextLoaderServlet.
    -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    			/WEB-INF/remoting-servlet.xml,
    			applicationContext.xml
    		</param-value>
    	</context-param>
    
    	<!--
    	- Loads the root application context of this web app at startup,
    	- by default from "/WEB-INF/applicationContext.xml".
    	- Note that it is preferable to use ContextLoaderListener in a servlet container
    	- that follows the Servlet 2.4 initialization order (most Servlet 2.3 containers do).
    	-
    	- Use WebApplicationContextUtils.getWebApplicationContext(servletContext)
    	- to access it anywhere in the web application, outside of the framework.
    	-
    	- The root context is the parent of all servlet-specific contexts.
    	- This means that its beans are automatically available in these child contexts,
    	- both for getBean(name) calls and (external) bean references.
    	-->
    
    	<servlet>
    		<servlet-name>context</servlet-name>
    		<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    
    	<!--
    	- Dispatcher servlet definition for HTTP remoting via Hessian, Burlap, and
    	- Spring's HTTP invoker (see remoting-servlet.xml for the controllers).
    	-->
    
    	<servlet>
    		<servlet-name>remoting</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>2</load-on-startup>
    	</servlet>
    
    	<!--
    	- Dispatcher servlet mapping for HTTP remoting via Hessian, Burlap, and
    	- Spring's HTTP invoker (see remoting-servlet.xml for the controllers).
    	-->
    	<servlet-mapping>
    		<servlet-name>remoting</servlet-name>
    		<url-pattern>/remoting/*</url-pattern>
    	</servlet-mapping>
    
    
    	<welcome-file-list>
    		<welcome-file>index.html</welcome-file>
    	</welcome-file-list>
    
    </web-app>
    jetty.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    	<bean id="Server" class="org.mortbay.jetty.Server" init-method="start" destroy-method="stop">
    		<property name="connectors">
    			<list>
    				<bean id="Connector" class="org.mortbay.jetty.nio.SelectChannelConnector">
    					<property name="port" value="8181"/>
    				</bean>
    			</list>
    		</property>
    		<property name="handler">
    			<bean id="handlers" class="org.mortbay.jetty.handler.HandlerCollection">
    				<property name="handlers">
    					<list>
    						<bean id="contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection">
    							<property name="handlers">
    								<list>
    									<bean class="org.mortbay.jetty.webapp.WebAppContext">
    										<property name="contextPath" value="/"/>
    										<!-- 
    										<property name="war" value="conf" />
    										-->
    										<property name="resourceBase" value="conf" />
    									</bean>
    								</list>
    							</property>
    						</bean>
    					</list>
    				</property>
    			</bean>
    		</property>
    	</bean>
    </beans>
    In the client side:

    HelloWorld.java

    Code:
    public interface HelloWorld {
    	public String sayhello();
    }
    TestHello.java
    Code:
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestHello {
    	public HelloWorld hello;
    	
    	public void sethello(HelloWorld hello){this.hello=hello;}
    	
    	public static void main(String[] args) {
    		ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {"app-client.xml"});
    		TestHello t=(TestHello)appContext.getBean("testHello");
    		System.out.println(t.hello.sayhello());
    	}
    }
    app-client.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    	<bean id="helloremotingHttpInvoker" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
    		<property name="serviceUrl" value="http://127.0.0.1:8181/remoting/helloWorld-httpinvoker" />
    		<property name="serviceInterface" value="HelloWorld" />
    	</bean>
    	
    	<bean id="testHello" class="TestHello">
    		<property name="hello" ref="helloremotingHttpInvoker"/>
    	</bean>
    </beans>
    When I run the server and after the client, Always I get the same error.

    Code:
    19-abr-2008 18:44:02 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1bd747e: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1bd747e]; startup date [Sat Apr 19 18:44:02 CEST 2008]; root of context hierarchy
    19-abr-2008 18:44:02 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [app-client.xml]
    19-abr-2008 18:44:02 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
    INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1bd747e]: org.springframework.beans.factory.support.DefaultListableBeanFactory@18ee9d6
    19-abr-2008 18:44:02 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18ee9d6: defining beans [helloremotingHttpInvoker,testHello]; root of factory hierarchy
    Exception in thread "main" org.springframework.remoting.RemoteAccessException: Could not access HTTP invoker remote service at [http://127.0.0.1:8181/remoting/helloWorld-httpinvoker]; nested exception is java.io.IOException: Did not receive successful HTTP response: status code = 404, status message = [Not Found]
    	at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.convertHttpInvokerAccessException(HttpInvokerClientInterceptor.java:211)
    Can somebody help me, please?

    Thanks in advance!!
    Last edited by ejml; Apr 19th, 2008 at 12:05 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    You need to remove the SimpleUrlHandlerMapping and set the name of your exporter to /helloWorld-httpinvoker. That should do the trick.

    Code:
    	<bean name="/helloWorld-httpinvoker" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
    		<property name="service"><ref bean="helloWorld"/></property>
    		<property name="serviceInterface"><value>HelloWorld</value></property>
    	</bean>
    You also might want to checkout the reference guide chapter 17.4 that explains how to use the HttpInvokerServiceExporter.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Hi Marten:

    I have trying this also. I get the same error.

    I have tested other code, I think that the problem is the Jetty version. I have found a jetty.xml for the 6.x version of Jetty. The code is:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans" 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-2.0.xsd">
    
    	<!-- http://svn.codehaus.org/jetty/jetty/trunk/extras/spring/etc/jetty-spring.xml -->
    	<!-- http://docs.codehaus.org/display/JETTY/Embedding+Jetty -->
    	<!-- http://jetty.mortbay.org/xref/org/mortbay/jetty/example/OneServletContext.html -->
    	<!-- http://ws.apache.org/xmlrpc/server.html -->
    
    	<bean name="jetty" class="org.mortbay.jetty.Server" init-method="start" destroy-method="stop">
    		<constructor-arg value="8080" />
    		<property name="handler">
    			<bean id="handlers" class="org.mortbay.jetty.handler.HandlerCollection">
    				<property name="handlers">
    					<list>
    						<!-- order is very important here; the contexts should come before anything else -->
    						<bean id="contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection">
    							<property name="handlers">
    								<list>
    									<bean class="org.mortbay.jetty.servlet.Context">
    										<property name="contextPath" value="/" />
    										<!-- <property name="resourceBase" value="src/main/resources" />  -->
    										<property name="resourceBase" value="conf" />
    										<property name="servletHandler">
    											<bean class="org.mortbay.jetty.servlet.ServletHandler">
    												<property name="servlets">
    													<list>
    														<!--
    														<bean class="org.mortbay.jetty.servlet.ServletHolder">
    															<property name="name" value="rmi" />
    															<property name="servlet">
    																<bean class="org.springframework.web.servlet.DispatcherServlet" />
    															</property>
    															<property name="initParameters">
    																<map>-->
    																	<!-- this path assumes that the "resourceBase" property above is set correctly -->
    																	<!-- <entry key="contextConfigLocation" value="net/gredler/prot/server/application-context-rmi.xml" />
    																</map>
    															</property>
    														</bean>
    														 -->
    														<bean class="org.mortbay.jetty.servlet.ServletHolder">
    															<property name="name" value="httpInvoker" />
    															<property name="servlet">
    																<bean class="org.springframework.web.servlet.DispatcherServlet" />
    															</property>
    															<property name="initParameters">
    																<map>
    																	<!-- this path assumes that the "resourceBase" property above is set correctly -->
    																	<entry key="contextConfigLocation" value="net/gredler/prot/server/application-context-http-invoker.xml" />
    																</map>
    															</property>
    														</bean>
    														<!--
    														<bean class="org.mortbay.jetty.servlet.ServletHolder">
    															<property name="name" value="hessian" />
    															<property name="servlet">
    																<bean class="org.springframework.web.servlet.DispatcherServlet" />
    															</property>
    															<property name="initParameters">
    																<map>-->
    																	<!-- this path assumes that the "resourceBase" property above is set correctly -->
    																	<!-- 
    																	<entry key="contextConfigLocation" value="net/gredler/prot/server/application-context-hessian.xml" />
    																</map>
    															</property>
    														</bean>
    														 -->
    														 <!--
    														<bean class="org.mortbay.jetty.servlet.ServletHolder">
    															<property name="name" value="burlap" />
    															<property name="servlet">
    																<bean class="org.springframework.web.servlet.DispatcherServlet" />
    															</property>
    															<property name="initParameters">
    																<map> -->
    																	<!-- this path assumes that the "resourceBase" property above is set correctly -->
    																	<!-- 
    																	<entry key="contextConfigLocation" value="net/gredler/prot/server/application-context-burlap.xml" />
    																</map>
    															</property>
    														</bean>
    														 -->
    														<!-- this servlet requires that the org/apache/xmlrpc/webserver/XmlRpcServlet.properties file exist -->
    														<!-- see http://ws.apache.org/xmlrpc/server.html for more info -->
    														<!-- 
    														<bean class="org.mortbay.jetty.servlet.ServletHolder">
    															<property name="name" value="xmlRpc" />
    															<property name="servlet">
    																<bean class="org.apache.xmlrpc.webserver.XmlRpcServlet" />
    															</property>
    															<property name="initParameters">
    																<map>
    																	<entry key="enabledForExtensions" value="true" />
    																</map>
    															</property>
    														</bean>
    														 -->
    													</list>
    												</property>
    												<property name="servletMappings">
    													<list>
    														<!-- 
    														<bean class="org.mortbay.jetty.servlet.ServletMapping">
    															<property name="servletName" value="rmi" />
    															<property name="pathSpec" value="/rmi/*" />
    														</bean>
    														 -->
    														<bean class="org.mortbay.jetty.servlet.ServletMapping">
    															<property name="servletName" value="httpInvoker" />
    															<property name="pathSpec" value="/httpInvoker/*" />
    														</bean>
    														<!-- 
    														<bean class="org.mortbay.jetty.servlet.ServletMapping">
    															<property name="servletName" value="hessian" />
    															<property name="pathSpec" value="/hessian/*" />
    														</bean>
    														<bean class="org.mortbay.jetty.servlet.ServletMapping">
    															<property name="servletName" value="burlap" />
    															<property name="pathSpec" value="/burlap/*" />
    														</bean>
    														<bean class="org.mortbay.jetty.servlet.ServletMapping">
    															<property name="servletName" value="xmlRpc" />
    															<property name="pathSpec" value="/xmlRpc/*" />
    														</bean>
    														 -->
    													</list>
    												</property>
    											</bean>
    										</property>
    									</bean>
    								</list>
    							</property>
    						</bean>
    						<bean id="defaultHandler" class="org.mortbay.jetty.handler.DefaultHandler" />
    						<bean id="requestLog" class="org.mortbay.jetty.handler.RequestLogHandler" />
    					</list>
    				</property>
    			</bean>
    		</property>
    	</bean>
    
    </beans>
    Now, I get other error:

    Code:
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jetty' defined in file [C:\Archivos de programa\eclipse\Proyectos\JettyEmbeddedServer\jetty.xml]: Cannot create inner bean 'handlers' of type [org.mortbay.jetty.handler.HandlerCollection] while setting bean property 'handler'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'handlers' defined in file [C:\Archivos de programa\eclipse\Proyectos\JettyEmbeddedServer\jetty.xml]: Cannot create inner bean 'contexts' of type [org.mortbay.jetty.handler.ContextHandlerCollection] while setting bean property 'handlers' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contexts' defined in file [C:\Archivos de programa\eclipse\Proyectos\JettyEmbeddedServer\jetty.xml]: Cannot create inner bean 'org.mortbay.jetty.servlet.Context#b42cbf' of type [org.mortbay.jetty.servlet.Context] while setting bean property 'handlers' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mortbay.jetty.servlet.Context#b42cbf' defined in file [C:\Archivos de programa\eclipse\Proyectos\JettyEmbeddedServer\jetty.xml]: Cannot create inner bean 'org.mortbay.jetty.servlet.ServletHandler#1960f05' of type [org.mortbay.jetty.servlet.ServletHandler] while setting bean property 'servletHandler'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mortbay.jetty.servlet.ServletHandler#1960f05' defined in file [C:\Archivos de programa\eclipse\Proyectos\JettyEmbeddedServer\jetty.xml]: Cannot create inner bean 'org.mortbay.jetty.servlet.ServletHolder#17e6a96' of type [org.mortbay.jetty.servlet.ServletHolder] while setting bean property 'servlets' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mortbay.jetty.servlet.ServletHolder#17e6a96' defined in file [C:\Archivos de programa\eclipse\Proyectos\JettyEmbeddedServer\jetty.xml]: Cannot create inner bean 'org.springframework.web.servlet.DispatcherServlet#1f42b49' of type [org.springframework.web.servlet.DispatcherServlet] while setting bean property 'servlet'; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.web.servlet.DispatcherServlet] for bean with name 'org.springframework.web.servlet.DispatcherServlet#1f42b49' defined in file [C:\Archivos de programa\eclipse\Proyectos\JettyEmbeddedServer\jetty.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    If you use Spring 2.5 you also need to include the spring-webmvc.jar and spring-web.jar. The DispatcherServlet isn't included in the spring.jar anymore. Next to that you really need to remove the SimpleUrlHandlerMapping (the Exporters will take care of the mapping themselves).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    Hi Marten,

    Thanks by your quick reply. I have been blinded with Jetty, I thought that the problem was Jetty and it appears that not is so. Ok, I'm going to review the library of my WEB-INF/lib folder. A last Question: The Jetty library not are needed on this folder.

    Thanks.

  6. #6

    Default

    Hi Marten,

    After of review all my libraries, now it's working fine. But I needed the SimpleUrlHandlerMapping, It doesn't work if I remove it.

    Thanks again.

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Strange because you really shouldn't be needing it, especially if you specify the correct name (not ID!) of your bean...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •