jetty 配置jndi_使用Jetty设置JNDI(嵌入式)

本文介绍了如何在嵌入式Jetty服务器中配置JNDI,包括设置jetty-plus、添加数据源XML片段到jetty.xml,以及在Spring中使用JNDIObjectFactoryBean。同时,还提及了如何通过VM参数开启Jetty的远程调试功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jetty 配置jndi

我在开发工作区上运行嵌入式Jetty,从而节省了一些恶性的编译和部署周期。 我与Jetty的合作不多,易用性使我着迷于它。 我需要设置JNDI才能检索与数据库相关的活动的连接池。 尽管某些地方有完整的文档,但大多数都是分散的。 因此,本帖子旨在成为您通过Jetty设置JNDI的一站式服务。 如果没有,请发表评论,我很乐意为您提供帮助。

因此,首先让我们看看如何设置Jetty以作为嵌入式服务器运行。 我的eclipse项目的文件夹结构如下:

etc文件夹将包含jetty所需的所有配置文件。 您可以从此处下载码头。 对于这个例子,我使用了jetty-6.1.26。

在给定的文件夹位置包括以下jar;

LIB jetty-xxxx.jar,jetty-util-xxxx.jar,servlet-api-xxjar
lib / plus jetty-plus-xxxx.jar
lib /命名 jetty-naming-xxxx.jar

对于我的示例,我已经设置了mysql,因此mysql-connector jar也包含在我的库路径中。

将Jetty安装的etc目录中的所有文件复制到eclipse项目的etc目录中。

为了启用JNDI,我们首先需要包括jetty-plus。 您可以通过多种方式执行此操作,例如将其作为运行时参数提供,包括在WEB-INF中自己的jetty-env.xml中,或将所需的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>

接下来,您需要将与数据源相关的XML片段添加到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;

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公开的数据源。 为了简便起见 ,我已经使用Spring的JNDIObjectFactoryBean配置了它。 需要注意的重要方面是Jetty所需的jndi提供程序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>

有了这些,您就可以配置JNDI并通过Spring的JNDI模板进行访问。 我感兴趣的另一件事是使用码头服务器进行远程调试。 经过一番搜索,我发现您需要在运行时配置中包括以下内容作为VM参数。

-Xdebug -Xnoagent -Xrunjdwp:transport = dt_socket,服务器= y,暂挂= n,地址= 8000

这将使您能够在端口8000上进行远程调试。如果有任何疑问,请务必发表评论,我将非常乐意为任何人提供帮助。 当然,如果您确实看到任何错误,也请留下答复,再次感谢您:)。

参考:My My Journey Through IT博客上,由我们的JCG合作伙伴 Dinuka Arseculeratne 通过 Jetty(嵌入式)设置JNDI


翻译自: https://www.javacodegeeks.com/2012/04/setting-up-jndi-with-jetty-embedded.html

jetty 配置jndi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值