在嵌入式Jetty下配置JNDI

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

下面是我的一个例子,这个例子我使用的是jetty-6.1.26 你可以通过这里下载jetty。 包括了如下jar包。 

libjetty-x.x.xx.jar, jetty-util-x.x.xx.jar,servlet-api-x.x.jar
lib/plusjetty-plus-x.x.xx.jar
lib/namingjetty-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文件中加入了如下片段。 

<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的数据源,其他数据库请看这个链接

<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。 

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和初始化上下文。

<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进行远程调试。我通过搜索后发现,你需要为你的虚拟机运行时添加如下参数:

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

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

OSChina原创翻译/原文链接

转载于:https://my.oschina.net/jack230230/blog/59680

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值