My Web application is polling a Database for changes and Showing those changes on a JSP. The Database is changed by a legacy System and no possibility to get an event on Data change.
My approach is to poll the Database and synchronizing the data with an Application bean. If the data has changed, I use a push mechanism to update the JSP page. The Thread that polls the database is spawned by the Application bean.
I’m already using spring in my Web application so I want to use the spring #Scheduled annotation because I don’t want to spawn a Thread and let Spring handle the periodical execution. But the the Method is not execute Periodically.
I'm using Tomcat and MS Sql Server.
My Application bean looks like this
#Named("agentData")
#Scope("application")
public class AgentDataBean implements Serializable {
...
#Scheduled(fixedRate=5000)
public void loadData() {
// do database poll
}
...
}
My Spring and Web Xml look like this
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>JavaServerFaces</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>#{login.theme}</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/config/database.properties" />
</bean>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource" p:driverClassName="${jdbc.driverClassName}"
p:password="${jdbc.password}" p:url="${jdbc.url}" p:username="${jdbc.username}" />
<!-- Activates annotaion-based bean configuration -->
<context:annotation-config />
<!-- needed is for #Configurable -->
<context:component-scan base-package="de.cpls.alo.dashboard" />
<tx:annotation-driven />
<!-- Activates #Scheduled and #Async annotations fpr scheduling -->
<task:annotation-driven executor="executorWithPoolSizeRange" scheduler="taskScheduler"/>
<task:executor id="executorWithPoolSizeRange"
pool-size="5-25"
queue-capacity="100"/>
<!-- Defines a ThreadPoolTaskScheduler instance with configurable pool size.
The id becomes the default thread name prefix. -->
<task:scheduler id="taskScheduler" pool-size="1"/>
This question is a solution approach to this Question
Is there a best practice for showing database changes on a JSP
My Solution, using a SchedulerComponent
#Named("scheduler")
#Component
public class SchedulerComponent {
private List<IScheduledComponent> components;
public SchedulerComponent() {
this.components = new ArrayList<IScheduledComponent>();
}
public void register(IScheduledComponent component){
this.components.add(component);
}
#Scheduled(fixedRate = 5000)
public void doAllWork() {
try {
for(IScheduledComponent component : this.components){
component.doWork();
}
System.out.println("SchedulerComponent.doAllWork()");
} catch (Exception e) {
e.printStackTrace();
}
}
}
An Interface for Scheduled Execution
public interface IScheduledComponent {
public void doWork();
}
An let the Data Bean Implement the Interface and Register to the SchedulerComponent
#Named("agentData")
#Scope("application")
public class AgentDataBean implements Serializable,IScheduledComponent {
...
public void doWork() {
// do database poll
}
...
}
Now i got Scheduled with Spring and i got FacesContext in AgentDataBean so i can Execute a Icefaces Push.
Related
I am trying to create a Spring RESTful API very a basic application from scratch, but I am not able to access the controller. I could access JSP file but not controller. I have tried with to annotate with #RestController as well but it didn't work. I am running on Tomcat 8.
Error is:
The origin server did not find a current representation for the target
resource or is not willing to disclose that one exists. WEB.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<context:component-scan
base-package="com.controller />
<mvc:default-servlet-handler />
</beans>
My Controller is
#Controller
public class TransactionControllerImpl{
#Autowired
private TransactionService transactionService;
#RequestMapping(method = RequestMethod.GET, value="/transaction")
#ResponseBody
public String getTransactionList() {
try {
System.out.println("from controller");
return "HEllow rahul";//transactionService.getTransactionList();
}
catch (Exception e) {
return null;
}
}
If you are creating application context separately, you should provide context param and value as location of your context.xml file.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
For your error the controller is not accessible,it might be due to :-
<context:component-scan
base-package="com.controller />
check if you have written correct base-package name or try using
<context:component-scan base-package="..package name..">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
Hope it helps.
Remove listener from web.xml because you have only one context xml. If you want to load multiple context xml add listener and context param.
I am trying to load my Spring application and I have only html files to render but it is not able show any html file. It is always showing 404 error. But when I am trying to run it directly html file using tomcat then it will able to show my html files.
Below is my directory structure
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>ankServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ankServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<context:annotation-config />
<!-- Resolves views selected for rendering by #Controllers to .htmlresources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/resources/html/" />
<beans:property name="suffix" value=".html" />
</beans:bean>
<context:component-scan base-package="com.ankit" />
</beans:beans>
HomeController.java
#Controller
public class HomeController {
#RequestMapping(value = "/login", method = RequestMethod.GET)
public String displayLogin(HttpServletRequest request, HttpServletResponse response)
{
return "login";
}
Try to change this:
<servlet-mapping>
<servlet-name>ankServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
To:
<servlet-mapping>
<servlet-name>ankServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Please check and a hint where I made a mistake when configuring the beginning of the project because I get the error " No mapping found for HTTP request with URI " . The following codes are added to the project.
1.web.xml
2.servlet - context.xml
3. # Controller
In addition, a link is added to the screen of my tree design.
http://zapodaj.net/17ab5a68373a9.png.html
1.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
2.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="pl.konrad.hh" />
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="pl.konrad.hh" />
<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:url="jdbc:mysql://localhost:3306/bazahealthhelper"
p:driverClassName="com.mysql.jdbc.Driver" p:username="konrad"
p:password="konrad">
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManager"></beans:bean>
<beans:bean id="entityManager"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource" p:persistenceXmlLocation="classpath:META-INF/persistence.xml"></beans:bean>
3.
#Controller
public class UserController {
#Autowired
private UserServiceImpl userServiceImpl;
#RequestMapping(value = "/home", method = RequestMethod.GET)
public String home(Model model) {
model.addAttribute("allUsers", userServiceImpl.allUsers() );
return "home";
}
}
1). Addin seervlet-context.xml:
<mvc:annotation-driven></mvc:annotation-driven>
2). change your servlet mapping in web.xml as below:
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I read both of this but they do not seem to work
Java Spring #Scheduled tasks executing twice
Spring #Scheduled is executing task twice when using annotations
I do not have #Configurable class, and I do not use applicationConetxt.xml.
My ScheduleTasks
#EnableScheduling
#Component
public class ScheduledTasks {
#Autowired
private ApplicationContext ctx;
#Scheduled(fixedRate=5000)
public void monitorRoom(){
System.out.println("--------------------");
}
}
I can see that when I run the app in tomcat, the task prints twice evrey 5 seconds.
Somebody says to remove
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
it works , but I feel this is not a solution. since this should be needed.(at least from the example from google)
Here is my web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>wodinow</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="wodinow.weixin.jaskey" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/pages/**" location="/resources/pages/" />
<mvc:resources mapping="/images/**" location="/resources/images/" />
<mvc:annotation-driven/>
</beans>
Remove the context loader listener. You only need that if you have to initialize a parent context. The contexts from the dispatcher servlet are standalone, but could access such parent contexts if needed, i.e. to retreive a service or dao.
I am using spring framework for writing a web service application. There are not html or jsp pages in the application. It is purely web service.
This is my Controller class
#RequestMapping("/*")
public class Opinion {
private FeedbackService fs;
public Opinion(FeedbackService fs){
this.fs=fs;
}
#RequestMapping(value="/givefeedback",method=RequestMethod.POST)
public void Login(HttpServletRequest request,HttpServletResponse response) throws IOException, ClassNotFoundException
{
ObjectInputStream in=new ObjectInputStream(request.getInputStream());
serialize.Feedback feedback=(serialize.Feedback)in.readObject();
fs.writeFeedback(feedback);
response.setStatus(200);
}
My mvc-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="poll_Web" class="sef.controller.Opinion">
<constructor-arg ref="feedbackService" />
</bean>
<context:component-scan base-package="sef.controller" />
</beans>
My web.xml
<servlet>
<servlet-name>opinionDispacher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value> classpath:repository-config.xml /WEB-INF/mvc-config.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>opinionDispacher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value> classpath:repository-config.xml /WEB-INF/mvc-config.xml
</param-value>
</context-param>
I am using the URL localhost:8080/feedback/givefeedback. The app is deployed as feedback.war. But the request is not at all forwarded to the controller. I am not sure why is this is happening. I am also not sure till which point the request goes in the chain.
You're missing
<mvc:annotation-driven/>
from your servlet context configuration. This element will discover #Controller beans and their #RequestMapping methods and register them as handlers.
You'll need to add the appropriate XML namespace and schema location for that namespace.
Make Opinion a #Controller and remove
#RequestMapping("/*")
it doesn't really serve any purpose.
You have also defined the Opinion bean twice in mvc-config.xml. Remove bean definition for bean id poll_Web.