2013-06-22 6 views
10

मैं अपने जावा सर्वलेट्स की सेवा के लिए टोमकैट का उपयोग कर रहा हूं और यह मेरे लिए बहुत अधिक है। मुझे बस सेवा करने की ज़रूरत है, अकेले सर्वलेट अनुरोध, कोई स्थैतिक सामग्री नहीं, न ही जेएसपी इत्यादि। इसलिए मैं एक सर्वलेट कंटेनर ढूंढ रहा था जिसे मेरे आवेदन में एम्बेड किया जा सकता है। मुझे लगा कि अगर जेटी को छीन लिया जाए और इसे अकेले एक सर्वलेट कंटेनर के रूप में इस्तेमाल किया जाए, तो यह अधिक स्केलेबल और छोटे स्मृति पदचिह्न पर कब्जा कर सकता है, [मुझे जेटी के 'वेब सर्वर' और अन्य हिस्सों की आवश्यकता नहीं है]। तो मेरे पास कुछ प्रश्न हैं,जेटी को एक सर्वलेट कंटेनर के रूप में एम्बेड करना

  1. मैं अकेले सर्वलेट अनुरोधों को पूरा करने के लिए जेट्टी को अपने आवेदन कोड में कैसे एम्बेड करूं?
  2. यदि मैं अपने आवेदन कोड में जेटी कोड एम्बेड करता हूं, तो क्या मैं जेटी संस्करणों को आसानी से अपग्रेड कर पाऊंगा?
  3. मैं यहाँ जेट्टी कोड मिला है, अगर मैं अपने अनुप्रयोग है, जो एक मैं स्रोत से उपयोग करना चाहिए, में जेट्टी के सर्वलेट कंटेनर एम्बेड करने के लिए है http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/snapshot/jetty-9.0.3.v20130506.tar.bz2, घाट-9.0.3.v20130506/घाट-सर्वलेट या घाट-9.0 .3.v20130506/जेटी-सर्लेट

मैं अपने अनुप्रयोगों के साथ एपीआई अनुरोधों की सेवा करना चाहता हूं और मैं मुख्य बाधाओं के रूप में प्रदर्शन और स्केलेबिलिटी की तलाश में हूं। और निश्चित रूप से सर्वलेट 3.0 समर्थन।

उत्तर

16

जो आप खोज रहे हैं वह जेट्टी को एक एम्बेडेड परिदृश्य में चला रहा है।

वहां कई उदाहरण उपलब्ध हैं जो दिखाते हैं कि आपके लक्ष्यों को पूरा करने के लिए आपको आवश्यक विभिन्न टुकड़ों को कैसे जोड़ना है।

embedded examples in the jetty source tree देखें।

रिकॉर्ड के लिए, जेटी स्टैंडअलोन वास्तव में कुछ स्टार्टअप और क्लासपाथ से संबंधित बूटस्ट्रैप्स के साथ एम्बेडेड जेटी है। यह वही कोड है, और मूल रूप से उसी तरह इकट्ठा किया जाता है।

चूंकि आपने कहा था कि आप सर्वलेट 3.0 चाहते हैं, जेएसपी में कोई रूचि नहीं है, यह सेटअप करना आसान है। (जेएसपी सेटअप करने के लिए trickier है, लेकिन संभव है)।

सर्वलेट 3.0 विशिष्ट एम्बेडिंग के लिए, जिथब में होस्ट किया गया एक पूर्ण उदाहरण प्रोजेक्ट है।

https://github.com/jetty-project/embedded-servlet-3.0

संक्षेप में, आप निम्नलिखित प्रवर्तन कोड होगा।

package com.company.foo; 

import org.eclipse.jetty.annotations.AnnotationConfiguration; 
import org.eclipse.jetty.plus.webapp.EnvConfiguration; 
import org.eclipse.jetty.plus.webapp.PlusConfiguration; 
import org.eclipse.jetty.server.Server; 
import org.eclipse.jetty.webapp.Configuration; 
import org.eclipse.jetty.webapp.FragmentConfiguration; 
import org.eclipse.jetty.webapp.MetaInfConfiguration; 
import org.eclipse.jetty.webapp.TagLibConfiguration; 
import org.eclipse.jetty.webapp.WebAppContext; 
import org.eclipse.jetty.webapp.WebInfConfiguration; 
import org.eclipse.jetty.webapp.WebXmlConfiguration; 

public class EmbedMe { 
    public static void main(String[] args) throws Exception { 
     int port = 8080; 
     Server server = new Server(port); 

     String wardir = "target/sample-webapp-1-SNAPSHOT"; 

     WebAppContext context = new WebAppContext(); 
     // This can be your own project's jar file, but the contents should 
     // conform to the WAR layout. 
     context.setResourceBase(wardir); 
     // A WEB-INF/web.xml is required for Servlet 3.0 
     context.setDescriptor(wardir + "WEB-INF/web.xml"); 
     // Initialize the various configurations required to auto-wire up 
     // the Servlet 3.0 annotations, descriptors, and fragments 
     context.setConfigurations(new Configuration[] { 
          new AnnotationConfiguration(), 
          new WebXmlConfiguration(), 
          new WebInfConfiguration(), 
          new TagLibConfiguration(), 
          new PlusConfiguration(), 
          new MetaInfConfiguration(), 
          new FragmentConfiguration(), 
          new EnvConfiguration() }); 

     // Specify the context path that you want this webapp to show up as 
     context.setContextPath("/"); 
     // Tell the classloader to use the "server" classpath over the 
     // webapp classpath. (this is so that jars and libs in your 
     // server classpath are used, requiring no WEB-INF/lib 
     // directory to exist) 
     context.setParentLoaderPriority(true); 
     // Add this webapp to the server 
     server.setHandler(context); 
     // Start the server thread 
     server.start(); 
     // Wait for the server thread to stop (optional) 
     server.join(); 
    } 
} 
+1

जेट्टी 9 और सर्वलेट एपीआई 3.1 का उपयोग करके अद्यतन संस्करण: https://github.com/jetty-project/embedded-servlet-3.1 – Kapep

संबंधित मुद्दे