gwt jetty jndi lookup fails

2019-05-05 03:28发布

问题:

I am using gwt with jetty but want use jndi for datasource so followed the documentation for eclipse gwt jetty jndi and did below to run my gwt app

Run my gwt app with following options in eclipse

 -noserver 
-remoteUI "${gwt_remote_ui_server_port}:${unique_id}"
-startupUrl myapp.html
-logLevel INFO 
-codeServerPort 9997
-war war\location 
-server com.myproject.MyCustomJettyLauncher 
com.my.apps.app

My jetty-env.xml under WEB-INF configuration

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "
http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
 <New id="MSSQLDS" class="org.mortbay.jetty.plus.jndi.Resource">
    <Arg></Arg>
    <Arg>jdbc/MSSQLDS</Arg>
    <Arg>
     <New class="net.sourceforge.jtds.jdbcx.JtdsDataSource">
                 <Set name="User">dbuser</Set>
                 <Set name="Password">pwd</Set>
                 <Set name="DatabaseName">mydatabase</Set>
                 <Set name="ServerName">localhost</Set>
                 <Set name="PortNumber">1433</Set>

     </New>
    </Arg>
   </New>
</Configure>  

When I run my app i get below error

Starting Jetty on port 8888
   [WARN] Failed startup of context com.healthfortis.MyCustomJettyLauncher$WebAppContextWithReload@2ed7c530{/,C:\workspace\hf-src\src\main\webapp}
javax.naming.NameNotFoundException; 

remaining name 'java:comp'

    at org.mortbay.naming.NamingContext.lookup(NamingContext.java:578)
    at org.mortbay.naming.NamingContext.lookup(NamingContext.java:680)
    at org.mortbay.naming.local.localContextRoot.lookup(localContextRoot.java:164)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at org.mortbay.jetty.plus.webapp.EnvConfiguration.createEnvContext(EnvConfiguration.java:51)
    at org.mortbay.jetty.plus.webapp.EnvConfiguration.configureWebApp(EnvConfiguration.java:103)
    at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1217)
    at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)
    at com.healthfortis.MyCustomJettyLauncher$WebAppContextWithReload.doStart(MyCustomJettyLauncher.java:459)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.Server.doStart(Server.java:222)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at com.healthfortis.MyCustomJettyLauncher.start(MyCustomJettyLauncher.java:660)
    at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:494)
    at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1058)
    at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:800)
    at com.google.gwt.dev.DevMode.main(DevMode.java:304)

looks like eclipsse/jetty is not able to find jndi datasource..any suggestions??

回答1:

There are two issues in the above configuration

  • -noserver in run option is to disable jetty from running. It is used to run Web Application on external server like tomcat etc. To run webapp on embedded jetty server in GWT development mode this option is not required.
  • In jetty-web.xml <Arg>jdbc/MSSQLDS</Arg> should be changed as <Arg>java:/comp/env/jdbc/MSSQLDS</Arg>

Steps to set up a JNDI datasource to run on embedded Jetty server in GWT Development mode is as follows. (I have used mysql datasource as example but steps are same for other datasources)

  • Add jetty-naming-*.jar, jetty-plus-*.jar to project build path as shown in following screen shot. Jetty lib available at /usr/share/jetty/lib may be used. One may use other web servers like tomcat in production they are not added to WEB-INF/lib dir. In case jetty is used for production env also then they may be added to WEB-INF/lib dir.


  • Add jetty-web.xml with following content to WEB-INF directory

    <Arg>java:/comp/env/jdbc/dev</Arg>
    <Arg>
        <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
            <Set name="Url">jdbc:mysql://localhost:3306/dev?autoReconnect=true
            </Set>
            <Set name="User">dev</Set>
            <Set name="Password">dev</Set>
        </New>
    </Arg>
    

  • Run the GWT project as Web Application. You will get javax.naming.NoInitialContextException

  • To rectify this go to Run Configurations and open the application's run configuration. Select Arguments tab and add following line to VM Arguments

    -Djava.naming.factory.initial=org.mortbay.naming.InitialContextFactory

Screen shot of Run Configuration



Save the settings and launch the webapp in development mode.



标签: gwt Jetty jndi