Is there a way to configure and setup Embedded Tomcat in Spring? I can do so easily with Jetty 7 that I created a standalone Java application that will start Jetty as webcontainer and finally JUnit test can call the BO via HTTPInvoker.
To me, it seems I have to write code to do so by using Tomcat?
Spring xml file
<!-- Manually start server after setting parent context. (init-method="start") -->
<bean id="jettyServer"
class="org.eclipse.jetty.server.Server"
init-method="start"
destroy-method="stop">
<property name="threadPool">
<bean id="ThreadPool"
class="org.eclipse.jetty.util.thread.ExecutorThreadPool">
<constructor-arg value="0" />
<!--property name="corePoolSize" value="${jetty.server.thread.pool.core.pool.size}"/>
<property name="maximumPoolSize" value="${jetty.server.thread.pool.max.pool.size}"/-->
</bean>
</property>
<property name="connectors">
<list>
<bean id="Connector"
class="org.eclipse.jetty.server.nio.SelectChannelConnector"
p:port="${jetty.server.port}"
p:maxIdleTime="${jetty.server.max.idle.time}"
p:acceptors="${jetty.server.acceptor.num}"
p:confidentialPort="${jetty.server.ssl.port}" />
</list>
</property>
<property name="handler">
<bean class="org.eclipse.jetty.server.handler.HandlerCollection">
<property name="handlers">
<list>
<bean class="org.eclipse.jetty.servlet.ServletContextHandler">
<property name="contextPath" value="/"/>
<property name="sessionHandler">
<bean class="org.eclipse.jetty.server.session.SessionHandler"/>
</property>
<property name="resourceBase" value="."/>
<property name="servletHandler">
<bean class="org.eclipse.jetty.servlet.ServletHandler">
<property name="servlets"> <!-- servlet definition -->
<list>
<!-- default servlet -->
<bean class="org.eclipse.jetty.servlet.ServletHolder">
<property name="name" value="DefaultServlet"/>
<property name="servlet">
<bean class="org.springframework.web.servlet.DispatcherServlet"/>
</property>
<property name="initParameters">
<map>
<entry key="contextConfigLocation" value="classpath:config/DefaultServlet-servlet.xml" />
</map>
</property>
</bean>
</list>
</property>
<property name="servletMappings">
<list><!-- servlet mapping -->
<bean class="org.eclipse.jetty.servlet.ServletMapping">
<property name="pathSpecs">
<list><value>/</value></list>
</property>
<property name="servletName" value="DefaultServlet"/>
</bean>
</list>
</property>
</bean>
</property>
</bean>
<bean class="org.eclipse.jetty.server.handler.RequestLogHandler">
<property name="requestLog">
<bean class="org.eclipse.jetty.server.NCSARequestLog">
<constructor-arg value="${jetty.server.log.dir}/jetty-yyyy_mm_dd.log"/>
<property name="extended" value="false"/>
</bean>
</property>
</bean>
</list>
</property>
</bean>
</property>
</bean>
DefaultServlet-servlet.xml
<!-- This default handler takes care of each of the services enumerated below -->
<bean id="defaultHandlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean id="helloService" class="com.company.ws.bo.HelloServiceImpl"/>
<!-- SpringHTTP Service Exposure -->
<bean name="/HelloService"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"
lazy-init="true">
<property name="service" ref="helloService" />
<property name="serviceInterface"
value="com.company.ws.bo.IHelloService" />
</bean>
Tomcat 7 can be used as an embedded Server. As far as I know there is no special spring support, but you don't need special spring support to start an tomcat out of an spring application.
#See:
this blog and this (german)
Related
I am creating a new Spring application to hold a SOAP Web Service, and decided to use Jetty instead of deploying to Tomcat.
So far, the application doesn't have any Starter class and Spring is managing the creation of the Beans.
I am planning to have Jetty start up the Spring context, however I have also heard of Spring Boot which could possibly start Jetty, so which is best ?
I have the below configuration, which is used elsewhere to start Jetty such as this in a Starter class:
ApplicationContext appContext = new ClassPathXmlApplicationContext("service-context.xml");
Server jetty = (Server) appContext.getBean("jettyServer");
However, I'm thinking this can be improved, and that I don't need a Starter class since Spring is creating everything. So, what is the best approach ? Thanks.
<bean id="jettyServer" class="org.eclipse.jetty.server.Server" destroy-method="stop">
<constructor-arg type="org.eclipse.jetty.util.thread.ThreadPool">
<bean id="ThreadPool" class="org.eclipse.jetty.util.thread.QueuedThreadPool">
<property name="minThreads" value="${jetty.min.threads}" />
<property name="maxThreads" value="${jetty.max.threads}" />
</bean>
</constructor-arg>
<property name="connectors">
<list>
<bean id="Connector" class="org.eclipse.jetty.server.ServerConnector">
<constructor-arg ref="jettyServer"/>
<property name="port" value="${jetty.port}" />
</bean>
</list>
</property>
<property name="handler">
<bean class="org.eclipse.jetty.server.handler.HandlerCollection">
<property name="handlers">
<list>
<bean class="org.eclipse.jetty.servlet.ServletContextHandler">
<property name="contextPath" value="/" />
<property name="sessionHandler">
<bean class="org.eclipse.jetty.server.session.SessionHandler" />
</property>
<property name="resourceBase" value="." />
<property name="servletHandler">
<bean class="org.eclipse.jetty.servlet.ServletHandler">
<property name="servlets"> <!-- servlet definition -->
<list>
<!-- default servlet -->
<bean class="org.eclipse.jetty.servlet.ServletHolder">
<property name="name" value="DefaultServlet" />
<property name="servlet">
<!--<bean class="org.eclipse.jetty.servlet.DefaultServlet"/> -->
<bean class="org.springframework.web.servlet.DispatcherServlet" />
</property>
<property name="initParameters">
<map>
<entry key="contextConfigLocation" value="classpath*:**/spring-servlet.xml" />
</map>
</property>
</bean>
</list>
</property>
<property name="servletMappings">
<list><!-- servlet mapping -->
<bean class="org.eclipse.jetty.servlet.ServletMapping">
<property name="pathSpecs">
<list>
<value>/</value>
</list>
</property>
<property name="servletName" value="DefaultServlet" />
</bean>
</list>
</property>
</bean>
</property>
</bean>
</list>
</property>
</bean>
</property>
</bean>
I was using Jetty 6.x where we created a spring based Jetty server with dirAllowed set to false.
The config is as follows.
<bean id="Server" class="org.mortbay.jetty.Server" init-method="start" destroy-method="stop">
<property name="connectors">
<list>
<bean id="Connector" class="org.mortbay.jetty.nio.SelectChannelConnector">
<property name="port" value="${tnplportal.jettyServer.httpPort}" />
<property name="headerBufferSize" value="${tnplportal.jettyServer.headerBufferSize}" />
</bean>
</list>
</property>
<property name="handler">
<bean id="handlers" class="org.mortbay.jetty.handler.HandlerCollection">
<property name="handlers">
<list>
<bean id="contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection">
<property name="handlers">
<list>
<bean class="org.mortbay.jetty.webapp.WebAppContext">
<property name="contextPath" value="/fileServer" />
<property name="resourceBase" value="ResourcePath" />
<property name="initParams">
<map>
<entry key="org.mortbay.jetty.servlet.Default.dirAllowed" value="false" />
</map>
</property>
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
</property>
</bean>
Now I am upgrading to Jetty 8.1.12 and found that initParams is not available for org.eclipse.jetty.webapp.WebAppContext.Now present config is below (with dirAllowed commented out)
<bean id="Server" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop">
<property name="connectors">
<list>
<bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<property name="port" value="${tnplportal.jettyServer.httpPort}" />
</bean>
</list>
</property>
<property name="handler">
<bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
<property name="handlers">
<list>
<bean id="contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection">
<property name="handlers">
<list>
<bean class="org.eclipse.jetty.webapp.WebAppContext">
<property name="contextPath" value="/fileServer" />
<property name="resourceBase" value="resourcePath" />
<!-- <property name="initParams">
<map>
<entry key="org.mortbay.jetty.servlet.Default.dirAllowed" value="false" />
</map>
</property> -->
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
</property>
</bean>
Can someone tell me how to set dirAllowed property for Jetty 8.1.12
I saw few posts for code based servers like this
But my server is spring based. How do I set with spring based configuration.
The upgrade from Jetty 6 to Jetty 8 requires you to update your Jetty references.
To start with, you'll need to update all of your named classes. The project moved to the Eclipse Foundation 6 years ago, this resulted in a mandatory package name change from org.mortbay.jetty to org.eclipse.jetty
Then you'll want to update the various setters to be relevant to what you are attempting to do.
Would recommend that you grab a copy of the Jetty Distribution tarball (or zip) and check out the Jetty XML files that it comes with for some inspiration, while also referencing the Jetty 8 Javadocs for some details.
Note: Jetty 6 was EOL'd in 2010. Jetty 8 is EOL at the end of 2014, there will be no more updates to Jetty 8 after this year. Would highly encourage that you upgrade all the way to Jetty 9 now.
An temp workaround should be create a custom WebAppContext, it's not grace but works.
public class CustomWebAppContext extends org.eclipse.jetty.webapp.WebAppContext{
public void setInitParams(Map<String, String> values){
Map<String, String> currectParams= getInitParams();
if(currectParams==null){
currectParams= new HashMap<String, String>();
}
for(Map.Entry<String,String> entry : values.entrySet()){
currectParams.put(entry.getKey(), entry.getValue());
}
}}
Then in xml :
<bean class="CustomWebAppContext">
<property name="contextPath" value="/fileServer" />
<property name="resourceBase" value="ResourcePath" />
<property name="initParams">
<map>
<entry key="org.mortbay.jetty.servlet.Default.dirAllowed" value="false" />
</map>
</property>
</bean>
I developed a login mechanism for one of my projects with Spring 2.5 , asegi security 1.0.7 and I used Tomcat 6 as my development server.When I was developing the project everything worked fine and I could successfully log-in.The problem begun when I deployed my application on the production server.From the moment I deployed the application on the production tomcat 6 I could not log-in even with the correct username and password and the most weird of all is that no exception is thrown.I just can't log -in!
here is the application-context.xml of the application:
<bean id="authedicationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="userDetailService"/>
</bean>
<bean id="authenticationEntryPoint" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
<property name="loginFormUrl" value="/login.htm" />
</bean>
<bean id="filterChainProxy"
class="org.acegisecurity.util.FilterChainProxy">
<property name="filterInvocationDefinitionSource">
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/**=authenticationProcessingFilter,exceptionTranslationFilter
</value>
</property>
</bean>
<bean id="authenticationProcessingFilter"
class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="authenticationFailureUrl" value="/error.htm" />
<property name="defaultTargetUrl" value="/admin_menu.htm" />
<property name="filterProcessesUrl" value="/j_acegi_security_check" />
</bean>
<bean id="roleVoter" class="org.acegisecurity.vote.RoleVoter"/>
<bean id="accessDecisionManager" class="org.acegisecurity.vote.UnanimousBased">
<property name="decisionVoters">
<list>
<ref bean="roleVoter"/>
</list>
</property>
<property name="allowIfAllAbstainDecisions" value="true"/>
</bean>
<bean id="filterSecurityInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="accessDecisionManager" ref="accessDecisionManager"/>
<property name="objectDefinitionSource">
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/add_article.htm=ROLE_ADMIN
/add_publication.htm=ROLE_ADMIN
/admin_menu.htm=ROLE_ADMIN
</value>
</bean>
</property>
</bean>
<bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager">
<property name="providers">
<list>
<ref bean="authedicationProvider"/>
</list>
</property>
</bean>
<bean id="userDetailService" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl">
<property name="dataSource" ref="datasource"/>
<property name="usersByUsernameQuery">
<value>
SELECT username,password,'true' AS enabled FROM Users where username=?
</value>
</property>
<property name="authoritiesByUsernameQuery">
<value>
SELECT username,role_name FROM Roles r,Users u WHERE r.user=u.id AND u.username=?
</value>
</property>
</bean>
Am I missing somthing?Any help would be really appreciated!Thank you in advance
Is this what you have <bean id="authe**d**icationProvider" in your first line of application file ?
I'm trying to create a webserver embedding jetty (rather than Java EE) , and map my servlets RESTfully, using jersey.
I'm using spring for dependency injection, and mapping the servlets as beans
However, when I try to make an HTTP req to the mapped servlets, i get error 500- server error, or 404, page not found.
I'm not sure if i'm doing this the right way, and I should probably be using the jetty.xml rather than this. (wondering if there's a shorcut using jetty.xml)
<bean id="contexts"
class="org.eclipse.jetty.server.handler.ContextHandlerCollection">
</bean>
<bean id="server" class="org.mortbay.jetty.spring.Server"
init-method="start" destroy-method="stop">
<property name="threadPool">
<bean id="ThreadPool" class="org.eclipse.jetty.util.thread.QueuedThreadPool">
<property name="minThreads" value="10" />
<property name="maxThreads" value="50" />
</bean>
</property>
<property name="connectors">
<list>
<bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<property name="port" value="8080" />
</bean>
</list>
</property>
<property name="handler">
<bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
<property name="handlers">
<list>
<ref bean="contexts" />
<bean class="org.eclipse.jetty.server.handler.ResourceHandler">
<property name="directoriesListed" value="true" />
<property name="welcomeFiles">
<list>
<value>index.jsp</value>
</list>
</property>
<property name="resourceBase" value="./WebContent" />
</bean>
<bean id="myServletHandler" class="org.eclipse.jetty.servlet.ServletHandler">
<property name="servlets">
<list>
<bean id="jerseyServletContainer" class="org.eclipse.jetty.servlet.ServletHolder">
<property name="name" value="jersey" />
<property name="servlet">
<bean class="com.sun.jersey.spi.container.servlet.ServletContainer" />
</property>
<property name="initParameters">
<map>
<entry key="com.sun.jersey.config.property.resourceConfigClass"
value="com.sun.jersey.api.core.PackagesResourceConfig" />
<entry key="com.sun.jersey.config.property.packages"
value="servlets" />
</map>
</property>
</bean>
</list>
</property>
<property name="servletMappings">
<list>
<bean id="jerseyMapping" class="org.eclipse.jetty.servlet.ServletMapping">
<property name="servletName" value="jersey" />
<property name="pathSpec" value="/*" />
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
</property>
<property name="beans">
<list>
<bean id="ContextDeployer" class="org.eclipse.jetty.deploy.ContextDeployer">
<property name="contexts" ref="contexts" />
<property name="directory" value="contexts" />
<property name="scanInterval" value="5" />
</bean>
</list>
</property>
</bean>
I have one class in the servlets package: DoNothing.java
package servlets;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/nothing")
public class doNothing
{
#GET
#Produces("text/plain")
public String returnNothing()
{
return ("test");
}
}
what in the world am I doing wrong? or should I ask what in the world am I doing right?
Thanks
I was looking for a way to use Jersey+Spring+Embedded Jetty and found this question. I tried your method and it actually works.
If you want to actually use Spring beans in your resources you can use jersey-spring:
<bean id="server" class="org.mortbay.jetty.Server" destroy-method="stop">
<property name="connectors">
<list>
<bean id="Connector" class="org.mortbay.jetty.nio.SelectChannelConnector">
<property name="port" value="8080"/>
</bean>
</list>
</property>
<property name="handlers">
<list>
<bean class="org.mortbay.jetty.servlet.Context">
<property name="contextPath" value="/"/>
<property name="sessionHandler">
<bean class="org.mortbay.jetty.servlet.SessionHandler" />
</property>
<property name="servletHandler">
<bean class="org.mortbay.jetty.servlet.ServletHandler">
<property name="servlets">
<list>
<bean class="org.mortbay.jetty.servlet.ServletHolder">
<property name="name" value="jersey" />
<property name="servlet">
<bean class="com.sun.jersey.spi.spring.container.servlet.SpringServlet" />
</property>
<property name="initParameters">
<map>
<entry key="com.sun.jersey.spi.container.ContainerRequestFilters"
value="com.sun.jersey.api.container.filter.LoggingFilter" />
<entry key="com.sun.jersey.spi.container.ContainerResponseFilters"
value="com.sun.jersey.api.container.filter.LoggingFilter" />
</map>
</property>
</bean>
</list>
</property>
<property name="servletMappings">
<list>
<bean class="org.mortbay.jetty.servlet.ServletMapping">
<property name="servletName" value="jersey"/>
<property name="pathSpecs">
<list>
<value>/*</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
</property>
<property name="eventListeners">
<list>
<bean id="requestContextListener" class="org.springframework.web.context.request.RequestContextListener"/>
<bean id="contextLoaderListener" class="org.springframework.web.context.ContextLoaderListener"/>
</list>
</property>
<property name="initParams">
<map>
<entry key="contextConfigLocation" value="classpath:META-INF/AdditionalBeansContext.xml"/>
</map>
</property>
</bean><!--
--></list>
</property>
</bean>
In file AdditionalBeansContext.xml:
Then define your Resources with: #Component annotation, injected beans with #Autowired
I'm using Spring,Quartz to schedule send email function.But I can't do that
This is my error
java.lang.NullPointerException
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTr ansport.java:557)
at org.springframework.mail.javamail.JavaMailSenderIm pl.doSend(JavaMailSenderImpl.java:382)
at org.springframework.mail.javamail.JavaMailSenderIm pl.send(JavaMailSenderImpl.java:291)
at org.springframework.mail.javamail.JavaMailSenderIm pl.send(JavaMailSenderImpl.java:277)
at org.vnitt.service.SendMailService.sendMail(SendMai lService.java:59)
at org.vnitt.shedule.SendMailJob.executeInternal(Send MailJob.java:47)
at org.springframework.scheduling.quartz.QuartzJobBea n.execute(QuartzJobBean.java:90)
at org.quartz.core.JobRunShell.run(JobRunShell.java:2 02)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run (SimpleThreadPool.java:525)
---And here is my config file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailS enderImpl">
<property name="defaultEncoding" value="UTF-8"/>
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="protocol" value="smtps" />
<property name="username" value="myemail#gmail.com"/>
<property name="password" value="mypass"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtps.auth">true</prop>
<prop key="mail.smtps.starttls.enable">true</prop>
<prop key="mail.smtps.debug">true</prop>
</props>
</property>
</bean>
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage" >
<property name="from" value="myemail#gmail.com" />
<property name="subject" value="Testing send email using Spring and Gmail " />
</bean>
<bean class="org.springframework.scheduling.quartz.Sched ulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronSendMailTrigger"/>
</list>
</property>
</bean>
<!-- end config Quartz to schdule a task -->
<!-- begin config to schedule SendMailService -->
<bean id="sendMailService"
class="org.service.SendMailService">
<property name="mailSender">
<ref bean="mailSender"/>
</property>
<property name="mailMessage">
<ref bean="mailMessage"/>
</property>
</bean>
<!-- config JobDetail -->
<bean id="sendMailJob"
class="org.springframework.scheduling.quartz.JobDe tailBean">
<property name="jobClass">
<value>org.shedule.SendMailJob</value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="sendMailService">
<ref bean="sendMailService"/>
</entry>
</map>
</property>
</bean>
<!-- config cron trigger -->
<bean id="cronSendMailTrigger"
class="org.springframework.scheduling.quartz.CronT riggerBean">
<property name="jobDetail">
<ref bean="sendMailJob"/>
</property>
<property name="cronExpression">
<value>0 0/1 * * * ?</value>
</property>
</bean>
<!-- end config to schedule SendMailService -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/spring_quartz_scheduler.vnitt">mainController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlVi ew</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Plz help me to solve this problem!
Thanks
EDIT
Thanks for your answer. I have fix my bug, I did not set recipient in my code and my configuration.That's cause.
Are you specifying recipients for your email message? You're not doing so in Spring context (to is not set). That's most likely the reason for NPE.
I am also faced the similar kind of issue and its due to no recipients.