How to execute a spring batch job by clicking on a button in a jsp page? - spring-mvc

I'm a newbie in spring batch and spring Mvc, and I want that a batch job (which extracts data from a database and writes it in another database) is executed from a jsp page by clicking on a button (or a link if it's possible) I'm using spring Mvc. This is my job configuration:
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseType" value="oracle" />
</bean>
<bean id="itemReader"
class="org.springframework.batch.item.database.JdbcCursorItemReader"
scope="step">
<property name="dataSource" ref="dataSource" />
<property name="sql"
value="select id,name,qual from users" />
<property name="rowMapper">
<bean class="tn.com.spring.UserRowMapper" />
</property>
</bean>
<bean id="oracleitemWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource" />
<property name="sql">
<value>
<![CDATA[
insert into users2(id,name,qual)
values (:id,:name,:qual)
]]>
</value>
</property>
<property name="itemSqlParameterSourceProvider">
<bean
class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
</property>
</bean>
<batch:job id="Job" job-repository="jobRepository">
<batch:step id="step1">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="itemReader" writer="oracleitemWriter"
commit-interval=" 10">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<jdbc:initialize-database data-source="dataSource">
<jdbc:script
location="org/springframework/batch/core/schema-drop-oracle10g.sql" />
<jdbc:script location="org/springframework/batch/core/schema-oracle10g.sql" />
</jdbc:initialize-database>
and this is my job controller (I found it in the net but still not working!)
#Component
#Controller()
public class JobController {
#Autowired
#Qualifier("JobLauncher")
private JobLauncher jobLauncher;
#Autowired
#Qualifier("Job")
private Job job;
#RequestMapping(value = "/job")
public void job() {
try {
JobExecution execution = jobLauncher.run(job, new JobParameters());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
and here is the button
<form action=" <%=application.getContextPath()%>/job" method="get">
<input type="submit" value="execute My job" />
</form>
Could you please help me ? Whats's missing in my configuration?
I'm confused !
Thanks in advance.

As a solution for this problem, I have made this javascript code which will invoke the execution of the job :
$(function() {
$('#JobBtn').click(function() {
$.get('${batchJobUrl}');
});
});
and in the jsp page you need to specify the Id of the button writen in the javascript code:
<input type="button" value="execute job " id='JobBtn' class="btn" />
I Hope it will help someOne..

Related

Switch on/off IBM MQ listener based on flag from DB

This is an existing monolith app and fix is required without a drastic change of the setup.
Project setup:
project 1 (config) - > where all mq-xml files are present (e.g - ibm_mq_config.xml)
e.g - content of dev_bm_mq.xml
${hname}
${port}
${qmgr}
1
<!-- JMS Queue Connection Factory -->
<bean id="jmsQueueIdsConnectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory">
<ref bean="mqIdsConnectionFactory" />
</property>
</bean>
<!-- JMS Destination Resolver -->
<bean id="jmsDestinationResolver"
class="org.springframework.jms.support.destination.DynamicDestinationResolver">
</bean>
<!-- JMS Queue Template -->
<bean id="jmsQueueIdsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref bean="jmsQueueIdsConnectionFactory" />
</property>
<property name="defaultDestinationName">
<value>${myQUEUE}</value>
</property>
<property name="pubSubDomain">
<value>false</value>
</property>
<property name="receiveTimeout">
<value>20000</value>
</property>
</bean>
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsQueueConnectionFactory" />
<property name="destinationName">
<value>${myQUEUE}</value>
</property>
<property name="messageListener" ref="simpleMessageListener" />
<property name="concurrentConsumers" value="2" />
<property name="maxConcurrentConsumers" value="3" />
<property name="idleTaskExecutionLimit" value="4" />
<property name="maxMessagesPerTask" value="4" />
<property name="receiveTimeout" value="5000" />
<property name="recoveryInterval" value="5000" />
<property name="sessionTransacted" value="true" />
</bean>
****Project B (App)****
loads the spring xml from project config as below:
WebContent/WEB-INF/spring/sprint-context.xm
<import resource="classpath*:com/my/package/${config.env}-${config-broker}.mq.xml"
public class TestMessageListener implements MessageListener {
public void onMessage(Message message) {
//process the message
}
}
When the server starts up, it's able to start the server and setup the listener without any issues.
Issue with the above setup : When we scale the app horizontally (add few nodes ), it's gives max channel issues which I am trying to solve.
Requirement:
based on a DB table I want to turn off the mq listener on few nodes on the fly. or when I horizontally scale the app.
e.g -
Table:mq-config
|host|broker|flag
-----------------------------
|qa5|ibm|false
|qa2|ibm|true
So, I want mq listener on qa5 not to start and qa2 to start and listen to the Queue. Also, I want to stop/start listener on the fly (just by updating the DB)
Question - Any thoughts on how do I achieve the above use case without re-writing the entire setup.
Inject the listener container (e.g. #Autowired).
Then
jmsContainer.stop();
jmsContainer.shutdown();
...
jmsContainer.initialize();
jmsContainer.start();
You can also set the autoStartup property to false to prevent the container from starting during application initialization (but don't call initialize() before the first start() - only after calling shutdown().

Spring Transaction Management with legacy JDBC

I am trying to inject Spring Transaction Management on to legacy JDBC code.But it fails in transaction management.
My Spring.xml
<tx:annotation-driven proxy-target-class="true"
transaction-manager="transactionManager" />
<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#sbsvmlx101.suntecsbs.com:1521:orcl11g" />
<property name="username" value="XLRT2_TEST_MAIN" />
<property name="password" value="XLRT2_TEST_MAIN" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
and annotated my service method with #Transactional.
#Transactional
public void createEmp(Employee employee, DataSourceTransactionManager dataSourceTransactionManager)throws Exception {
generaldao.createEmployee(employee,dataSourceTransactionManager);
}
I am trying to insert data into two different tables.First one got successfully inserted,and knowingly i made an error in second insertion ,expecting that the both insertions won't happen.
But surprisingly ,the first insertion got committed, and second has shown oracle error.
DAO
con = DataSourceUtils.doGetConnection(dataSource);
con.setAutoCommit(false);
insert = con.prepareStatement
("insert into emp500 values(?,?,?)");
insert.setInt(1, 1);
insert.setString(2, "test");
insert.setString(3, "data");
insert.execute();
insertdept = con.prepareStatement
("insert into department values(?,?)");
insertdept.setInt(1, 2);
insertdept.setString(2, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); //too large value for the column
insertdept.execute();
Please advice.
Thanks in Advance

Thymeleaf View not recognizing the specified action view

I am facing an issue with viewresolver. Thymeleaf viewresolver not picking up the specified view in my controller action method and it is taking action name as view path instead.
Below is my thymeleaf configuration and controller code.
Controller :
#ModelAttribute("user")
#RequestMapping(method = RequestMethod.POST, value = "/register")
public String register(Model model, #Valid User user, BindingResult result, HttpServletRequest request, final Locale locale) {if (result.hasErrors()) {
List<ObjectError> errors = result.getAllErrors();
for (ObjectError error : errors) {
log.error("Errors are :: " + error.getDefaultMessage());
}
return "registration/indexed";
}else{------my operations------return "profile/index"}}
thymeleaf config..
<!-- THYMELEAF: Template Resolver for email templates -->
<bean id="emailTemplateResolver"
class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
<property name="prefix" value="mail/" />
<property name="templateMode" value="HTML5" />
<property name="order" value="2" />
<!-- Template cache is true by default. Set to false if you want -->
<!-- templates to be automatically updated when modified. -->
<property name="cacheable" value="true" />
</bean>
<!-- Thymeleaf template resolver -->
<bean id="webTemplateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="order" value="1" />
</bean>
<!-- THYMELEAF: Template Engine (Spring3-specific version) -->
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolvers">
<set>
<ref bean="emailTemplateResolver" />
<ref bean="webTemplateResolver" />
</set>
</property>
</bean>
<bean id="thymeleafViewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
<property name="characterEncoding" value="UTF-8" />
</bean>
But my Thymeleaf not recognizing "registration/index" and it is searching for "registration/register"
Would anybody please suggest what I have to do??
Thanks & Regards,
Gupta Katakam
Finally I figured out the problem. The problem is due to #ModelAttribute("user") . When I removed this line on top of my action method the problem got resolved.
Thanks & Regards,
Gupta Katakam

How to enable #JsonRootName in spring mvc 3.2

I'm using Spring 3.2 and my Spring MVC controller generate JSON data (with jackson-databind-2.2.0). I would like to customize my JSON root name with #JsonRootName (com.fasterxml.jackson.annotation.JsonRootName) annotation, however, I could not figure out how to enable it with Spring configuration.
#JsonRootName("rootNameTest")
public class MyModel {
private String prop;
public String getProp() {
return prop;
}
public void setProp(String prop) {
this.prop = prop;
}
}
Here's my settings in sevlet-context.xml
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="contentNegotiationManager">
<bean class="org.springframework.web.accept.ContentNegotiationManager">
<constructor-arg>
<bean class="org.springframework.web.accept.ParameterContentNegotiationStrategy">
<constructor-arg>
<map>
<entry key="json" value="application/json"/>
</map>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
</list>
</property>
</bean>
Please help. Thanks.
Setbelow in com.fasterxml.jackson.databind.ObjectMapper
om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
om.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
this may be done by extending above Class with your custom and inject in org.springframework.http.converter.json.MappingJackson2HttpMessageConverter

Rss feeds and apache tiles with Spring MVC

I;m trying to create RSS feeds for my web site. I follow the tutorial from mkyong (http://www.mkyong.com/spring-mvc/spring-3-mvc-and-rss-feed-example/) which was quite useful. According to this tutorial i create a model class and the following class
public class CustomRssViewer extends AbstractRssFeedView{
#Override
protected void buildFeedMetadata(){
//some code
}
#Override
protected List<Item> buildFeedItems(){
//some code
}
}
And finally the controller class
#Controller
public class RssController {
#RequestMapping(value="/rssfeed", method = RequestMethod.GET)
public ModelAndView getFeedInRss() {
//set the RSS content
ModelAndView mav = new ModelAndView();
mav.setViewName("rssViewer");
mav.addObject("feedContent", items);
return mav;
}
}
According to the tutorial the View rssViewer belongs the class CustomRssViewer , so i need to write it at the dispatcher servlet the following lines of code:
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="rssViewer" class="com.mkyong.common.rss.CustomRssViewer" />
My problem is that i'm using apache tiles. So the rssViewer can not be recognised as i didn't enhanced it to the tiles definition. And i really don't know how can i do this. For example i need to write something as the following:
<definition name="rssViewer" template="?">
<put-attribute name="title" value=""/>
<put-attribute name="content" value=""/>
</definition>
At the template i don't know what to declare as well as at the put-attribute.Because until now at the template i use to declare the direction that a specific jsp exists. Something like this:
template="/WEB-INF/pages/mypage.jsp"
And also at the view-properties i don't know what should i declare.
Thanks in advance for any comment or response.
You should use a ContentNegotiatingViewResolver in conjuction with that example's BeanNameViewResolver. Just declare the order property of your already existing BeanNameViewResolver to be 1, and set the order property of the new ContentNegotiatingViewResolver to 0.
You should then configure the ContentNegotiatingViewResolver to use the appropriate View for RSS, and set the media type for RSS.
Here is an example from the Spring Docs:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="atom" value="application/atom+xml"/>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
Note, they are using atom, not RSS, but the idea is the same. Also they do not set the order (which you should do).

Resources