在嵌入式Jetty下配置JNDI

    我在我我的的开发环境中使用嵌入式jetty(查看jetty文档),以此来避免编译、部署等循环对时间的浪费。我使用jetty的时间并不长,可是他的易用性让我很喜欢。为了个人数据库链接池的活动链接,我须要创建起一套JNDI。这篇文章的目的就是让你学会在jetty下配置jndi。 html

下面是个人一个例子,这个例子我使用的是jetty-6.1.26 你能够经过这里下载jetty。 包括了以下jar包。 java

lib jetty-x.x.xx.jar, jetty-util-x.x.xx.jar,servlet-api-x.x.jar
lib/plus jetty-plus-x.x.xx.jar
lib/naming jetty-naming-x.x.xx.jar

    对于个人例子来说,我已经设置好了mysql查看mysql文档并且mysql驱动也在个人库路径下。 将jetty安装目录下的etc文件夹下的全部内容复制到eclipse工程目录下的etc目录里。 为了配置JNDI,咱们首先须要引入jetty-plus。固然有不少方法能够作到这一点,例如经过参数的方式、在jetty-env.xml中引入的方式、从jetty-plus.xml文件中复制相关内容到jetty.xml文件中等方式。我选择了最后一种,我在jetty.xml文件中加入了以下片断。 mysql

<Array id="plusConfig" type="java.lang.String">
  <Item>org.mortbay.jetty.webapp.WebInfConfiguration</Item>
  <Item>org.mortbay.jetty.plus.webapp.EnvConfiguration</Item>
  <Item>org.mortbay.jetty.plus.webapp.Configuration</Item>
  <Item>org.mortbay.jetty.webapp.JettyWebXmlConfiguration</Item>
  <Item>org.mortbay.jetty.webapp.TagLibConfiguration</Item>
</Array>

<call name="addLifeCycle">
      <arg>
        <new class="org.mortbay.jetty.deployer.WebAppDeployer">
          <set name="contexts"><ref id="Contexts"></ref></set>
          <set name="webAppDir"><systemproperty default="." name="jetty.home">/webapps</systemproperty></set>
          <set name="parentLoaderPriority">false</set>
          <set name="extract">true</set>
          <set name="allowDuplicates">false</set>
          <set name="defaultsDescriptor"><systemproperty default="." name="jetty.home">/etc/webdefault.xml</systemproperty></set>
          <set name="configurationClasses"><ref id="plusConfig"></ref></set>
        </new>
      </arg>
</call>

下一步,你须要将数据源(data source)添加到你的jetty.xml中。我添加了mysql的数据源,其余数据库请看这个连接web

<New id="myds" class="org.mortbay.jetty.plus.naming.Resource">

     <Arg>jdbc/MySQLDS</Arg>
     <Arg>
        <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
           <Set name="Url">jdbc:mysql://localhost:3306/test</Set>
           <Set name="User">root</Set>
           <Set name="Password">password</Set>
        </New>
     </Arg>
</New>

     如今,咱们已经设置好了全部的东西,你仅仅须要的就是在嵌入式环境中运行jetty。下面的代码展现了如何在main方法中以嵌入式的方式运行jetty。 spring

import java.io.File;

import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.handler.HandlerList;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.xml.XmlConfiguration;




public class JettyTest {

 public static void main(String[] args) throws Exception {
  Server jetty = new Server();
  String[] configFiles = {"etc/jetty.xml"};
  for(String configFile : configFiles) {
  XmlConfiguration configuration = new XmlConfiguration(new File(configFile).toURI().toURL());
  configuration.configure(jetty);
  }
  
  WebAppContext appContext = new WebAppContext();
  appContext.setContextPath("/myapp");
  File rd = new File("path_to_your_war_file");
  appContext.setWar(rd.getAbsolutePath());
  HandlerList handlers = new HandlerList();
  handlers.setHandlers(new Handler[]{ appContext, new DefaultHandler()});
  jetty.setHandler(handlers);
  jetty.start();
 }
}

    如今你能够经过jetty暴漏出的接口查看data source。简单的,我已经使用Spring的JNDIObjectFactoryBean配置好了。须要注意的一点是,jetty须要提供url和初始化上下文。sql

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
      <property name="environment">
          <props>
              <prop key="java.naming.factory.initial">org.mortbay.naming.InitialContextFactory</prop>
              <prop key="java.naming.provider.url">org.mortbay.naming</prop>
          </props>
      </property>
  </bean>
  
  <bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
 
      <property name="jndiTemplate">
        <ref bean="jndiTemplate"/>
      </property>
      <property name="jndiName">
            <value>jdbc/MySQLDS</value>
      </property>
    </bean>

     如今你已经能够经过Spring 的JNDI template来访问你配置好的JNDI了。我感兴趣的另一件事是使用jetty进行远程调试。我经过搜索后发现,你须要为你的虚拟机运行时添加以下参数:shell

-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000

 这些参数可让你在8000端口上进行远程调试。数据库

OSChina原创翻译/原文连接api

相关文章
相关标签/搜索