I have a web-app that i run through Maven Jetty plugin.
I configure it using a jetty.xml file. My problem comes when i want to set a custom authenticator that i have created in the WebAppContext. The XML config looks like below:
<New id="webAppContext" class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/</Set>
<Set name="copyWebDir">true</Set>
<Set name="extractWAR">false</Set>
</Set>
<Get name="securityHandler">
<Set name="authenticator">
<New class="MY_CUSTOM_AUTHENTICATOR">
</New>
</Set>
</Get>
</New>
When i run the above i get a ClassNotFoundException for "MY_CUSTOM_AUTHENTICATOR" class. I have to add that the class exists in the same maven project that i launch Jetty from.
Is there an "import" statement that i have to do in order for Jetty to load my class?
Thank you.
You most likely need to have your authenticator in an artifact that is then declared as a dependency of the jetty maven plugin itself. With classloader isolation in play the authenticator is probably in your webapp where the security handler does not have visibility.
Related
I'm trying to configure a jetty-servlet in java using maven. I've create a jetty.xml file with the following entries
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Set name="ThreadPool">
<New class="org.eclipse.jetty.util.thread.QueuedThreadPool">
<Set name="minThreads">10</Set>
<Set name="maxThreads">200</Set>
<Set name="detailedDump">false</Set>
</New>
</Set>
</Configure>
upon running the server
mvn jetty:run
I see the error
[WARNING] Config error at <Set name="ThreadPool">
<New class="org.eclipse.jetty.util.thread.QueuedThreadPool"><Set
name="minThreads">10</Set><Set name="maxThreads">200</Set><Set
name="detailedDump">false</Set></New>
</Set>
but cannot figure out what the problem actually is. I've also included jetty-util as a dependency in the pom.xml file. I'm using jetty version 9.4.12.v20180830 and java 8. Thanks for any help!
ThreadPool is a constructor argument for Server.
See: Javadoc for org.eclipse.jetty.server.Server
It's not a field and/or setter on Server, so you cannot use the <Set name="ThreadPool"> syntax.
Instead of replacing the threadpool, just "Get" the existing one and change settings on it.
Eg:
<Get name="ThreadPool">
<Set name="minThreads" type="int">10</Set>
<Set name="maxThreads" type="int">200</Set>
<Set name="detailedDump">false</Set>
</Get>
So, I am starting up Jetty using the Spring Context, in a standalone java application.
The deployment environment however uses an Apache server for load balancing Web requests to different instances of this application.
Unfortunately, I don't know much about Apache Load Balancing, however things are made simpler for me, and all I have to do is define a 'worker name' for the Jetty instance, probably in the Spring Context file; can anyone help me do this ?
I am shown the below configuration where I should probably be matching the 'jetty2' worker name in my Jetty configuration found in the spring context xml file:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Get name="sessionHandler">
<Get name="sessionManager">
<Call name="setIdManager">
<Arg>
<New class="org.eclipse.jetty.server.session.HashSessionIdManager">
<Set name="WorkerName">jetty2</Set>
</New>
</Arg>
</Call>
</Get>
</Get>
</Configure>
I'm trying to set up a JNDI DataSource in Jetty. In the start.ini file I have add the jetty-plus.xml file and the Options look like this:
OPTIONS=Server,resources,websocket,ext,plus,annotations
Then I'm adding the resource in my context file:
<New id="Traildevils" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg/>
<Arg>jdbc/Traildevils</Arg>
<Arg>
<New class="org.postgresql.ds.PGConnectionPoolDataSource">
<Set name="User">recom</Set>
<Set name="Password">recom</Set>
<Set name="DatabaseName">Traildevils</Set>
<Set name="ServerName">localhost</Set>
<Set name="PortNumber">5432</Set>
</New>
</Arg>
</New>
The postgresql-9.1-901.jdbc4.jar file is in the WEB-INF/lib of my war file.
When I'm now trying to start the jetty I get the following error:
2012-04-12 12:58:09.723:WARN:oejx.XmlConfiguration:Config error at
<New id="Traildevils" class="org.eclipse.jetty.plus.jndi.Resource"><Arg/>
<Arg>jdbc/Traildevils</Arg>
<Arg>|???<New class="org.postgresql.ds.PGConnectionPoolDataSource">
<Set name="User">recom</Set>
<Set name="Password">recom</Set>
<Set name="DatabaseName">Traildevils</Set>
<Set name="ServerName">localhost</Set><Set name="PortNumber">5432</Set></New>|??</Arg></New>
java.lang.ClassNotFoundException: org.postgresql.ds.PGConnectionPoolDataSource
What did I miss?
You need to add postgresql-9.1-901.jdbc4.jar to $JETTY_HOME/lib/ext.
This is because jetty initializes the JNDI context before it loads the classes from your WAR.
I'm developing a web application and I run Jetty as the development and testing environment when I develop under Eclipse.
When I make changes to Java classes, Eclipse automatically compiles them to the build directory, but Jetty won't see the changes until I stop and start the server. I know that Jetty supports "hot deployment" using ContextDeployer that will refresh updated application contexts, but it relies on a context file in a context directory being updated - which is not very useful in my case.
Is there a way to set up Jetty so that it will reload the web app when any of the classes it uses is updated?
My current jetty.xml looks something like this:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Set name="ThreadPool"><!-- bla bla --></Set>
<Call name="addConnector"><!-- bla bla --></Call>
<Set name="handler">
<New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<New id="webapp" class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="displayName">My Web App</Set>
<Set name="resourceBase">src/main/webapp</Set>
<Set name="descriptor">src/main/webapp/WEB-INF/web.xml</Set>
<Set name="contextPath">/mywebapp</Set>
</New>
</Item>
<Item>
<New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
</Item>
</Array>
</Set>
</New>
</Set>
</Configure>
We have not found a way of doing this (aside from implementing our own version of the org.eclipse.jetty.deploy.providers.WebAppProvider).
We have configured jetty to hot deploy webapps from the webapps folder (property monitoredDirName of the WebappDeployer).
Then to hot deploy, I recreate my link in this folder to the src/main/webapp folder of my Eclipse project. The linked must be suffixed .war.
Not really automatic but good enough and avoids a Jetty restart.
If you go the route of re-implementing a WebappDeployer, I would not monitor the changes in .class files - they change too much when compiled by Eclipse, particularly in the case of automatic builds. I would implement a 'Tomcat like' solution by monitoring changes to the web.xml file. Then a dummy change saved to this file from Eclipse would trigger a redeployment.
It is also possible to configure your jetty app with maven and starting periodical builds with Jenkins (even every couple of seconds, depending on the maschine you are working on)
I have used Jetty in the past but I have little experience with jWebSocket. I would like to add to my current program, which uses the Jetty libraries, and make it also support WebSocket connections though port 80. I have read it can be done but find little to no source or examples to read about it. Any help is appreciated.
I am currently working on the same thing, and so far I have found their task for this on Google Code:
http://code.google.com/p/jwebsocket/issues/detail?id=76
This was posted back in April of 2011:
"There's a separate project jWebSocketJetty available now in the Downloads / Nightly Build Section of jWebSocket.org now."
If you pull up the web.xml from that project, it looks like they've gotten their jwebsocket servlet working with jetty. I'll be looking into this more tomorrow.
You have to modify two configuration files to run jWebSocket on jetty using port 80.
1:- Modify your jWebSocket.xml and add jetty engine entry at top of engine section of xml.
<engines>
<engine>
<name>org.jwebsocket.jetty.JettyEngine</name>
.
.
</engine>
</engines>
You can delete all other engine entries.
2:- Modify jetty.xml. This file can be located at jWebSocketJetty\src\main\resources folder.
Modify first connector entry and set jetty.port property to 80.
<Call name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<Set name="host">
<Property name="jetty.host" />
</Set>
<!-- Jetty default -->
<!--
<Set name="port">
<Property name="jetty.port" default="80"/>
</Set>
-->
<!-- jWebSocket default, can be changed to 80 -->
<!-- but consider to update jWebSocket.js accordingly! -->
<Set name="port">
<Property name="jetty.port" default="80"/>
</Set>
<Set name="maxIdleTime">300000</Set>
<Set name="Acceptors">2</Set>
<Set name="statsOn">false</Set>
<Set name="confidentialPort">443</Set>
<Set name="lowResourcesConnections">20000</Set>
<Set name="lowResourcesMaxIdleTime">5000</Set>
<Set name="responseBufferSize">65536</Set>
</New>
</Arg>
</Call>
I don't know, because I haven't used it, but would their JettyServlet would work?