Using "org.springframework.batch.admin.integration.JobNameToJobRestartRequestAdapter" restart Batch Job - java

Can anyone provide me the details as how to implement the "JobNameToJobRestartRequestAdapter" Class API for restarting the failed job by providing the name of the job that needs to be executed.
I created the context-xml file
<int:channel id="job-launches" />
<int:channel id="job-restarts" />
<int:service-activator id="restartJobClassProperties" input-channel="job-restarts" output-channel="job-requests">
<bean class="org.springframework.batch.admin.integration.JobNameToJobRestartRequestAdapter">
<property name="jobLocator" ref="jobRegistry" />
<property name="jobExplorer" ref="jobExplorer" />
</bean>
</int:service-activator>
<bean id="jobRegistry" class="org.springframework.batch.core.configuration.JobLocator">
<property name="name" value="JobNameGoesHere" />
</bean>
<bean id="jobExplorer" class="org.springframework.batch.core.explore.JobExplorer" />
While executing the main class which is reading this context.xml file I am getting below error:
Error creating bean with name
'org.springframework.integration.config.ServiceActivatorFactoryBean#0':
Cannot create inner bean
'org.springframework.batch.admin.integration.JobNameToJobRestartRequestAdapter#0'
of type
[org.springframework.batch.admin.integration.JobNameToJobRestartRequestAdapter]
while setting bean property 'targetObject'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'org.springframework.batch.admin.integration.JobNameToJobRestartRequestAdapter#0'
defined in class path resource
[META-INF/spring/restart-job-context.xml]: Cannot resolve reference to
bean 'jobRegistry' while setting bean property 'jobLocator'; nested
exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'jobRegistry' defined in class path
resource [META-INF/spring/restart-job-context.xml]: Instantiation of
bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class
[org.springframework.batch.core.configuration.JobLocator]: Specified
class is an interface
I want to implement the functionality for defining the batch job name which I would like to restart and the properties can be extracted from the jobexplorer based on the last executed step.

JobLocator is an interface. You can't define a bean that is just an interface. I think you want to use a JobRegistry. Below is what Spring Batch Admin uses:
<bean id="jobLoader" class="org.springframework.batch.core.configuration.support.AutomaticJobRegistrar">
<property name="applicationContextFactories">
<bean class="org.springframework.batch.core.configuration.support.ClasspathXmlApplicationContextsFactoryBean">
<property name="resources" value="classpath*:/META-INF/spring/batch/jobs/*.xml" />
</bean>
</property>
<property name="jobLoader">
<bean class="org.springframework.batch.core.configuration.support.DefaultJobLoader">
<property name="jobRegistry" ref="jobRegistry" />
</bean>
</property>
</bean>
<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
The jobRegistry bean is a registry for the jobs to be looked up from (it does implement JobLocator. The jobLoader registers job definitions with the registry.

Related

Spring how to set proxy property dynamically to rest template

I am trying to set the proxy to spring rest template bean based on the property, I tried below configuration,
<bean id="forwardProxy" class="java.net.Proxy">
<constructor-arg>
<util:constant static-field="java.net.Proxy.Type.HTTP"/>
</constructor-arg>
<constructor-arg>
<bean class="java.net.InetSocketAddress">
<constructor-arg value="${forward.proxy.host}"/>
<constructor-arg value="${forward.proxy.port}"/>
</bean>
</constructor-arg>
</bean>
<util:constant id="noProxy" static-field="java.net.Proxy.NO_PROXY" />
<bean id="myRestTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg>
<bean class="org.springframework.http.client.SimpleClientHttpRequestFactory">
<property name="connectTimeout" value="5000"/>
<property name="readTimeout" value="5000"/>
<property name="proxy" ref="#{ ${aem.enableProxy}==true ? forwardProxy : noProxy }"/>
</bean>
</constructor-arg>
</bean>
but I am getting below error saying No bean named 'DIRECT' available
Caused by (repeated) ... : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRestTemplate' defined in class path resource [environment/application.xml]:
Cannot create inner bean 'org.springframework.http.client.SimpleClientHttpRequestFactory#1177bca' of type [org.springframework.http.client.SimpleClientHttpRequestFactory] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.http.client.SimpleClientHttpRequestFactory#1177bca' defined in class path resource [environment/application.xml]:
Cannot resolve reference to bean '#{ false==true ? forwardProxy : noProxy }' while setting bean property 'proxy'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'DIRECT' available
Proxy.Type.DIRECT is a enum, not sure how to create a bean for that. Is it possible to achieve what I am doing without creating a factory beans option.
Using spring version: 4.3.8

Multiple Persistent Units JPA

I have a Spring application, which is configured for multiple Persistent units.
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list value-type="java.lang.String">
<value>classpath*:META-INF/*-persistence.xml</value>
</list>
</property>
<property name="dataSources">
<map>
<entry key="dataSource" value-ref="dataSource" />
</map>
</property>
<property name="defaultDataSource" ref="dataSource" />
<property name="defaultPersistenceUnitName" value="pm-model" />
</bean>
However, I'm getting an error on startup saying that one of the persistent units is not able to load. I also called out in the code above to list all the persistence.xml files, but still getting the same error.
ERROR 2017-08-10 15:33:44,882 [org.springframework.web.context.ContextLoader] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bService': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com-model' is defined
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:307)
If anyone has any suggestions, I'm all ears. Thanks in advance.

I can not have two datasources configured JPA Spring XML WildFly

Spring config in xml
<jee:jndi-lookup id="datas1" expected-type="javax.sql.DataSource"
jndi-name="java:/jndi1" />
<jee:jndi-lookup id="datas2" expected-type="javax.sql.DataSource"
jndi-name="java:/jndi2" />
<bean id="EntityM"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="datas1" />
<property name="packagesToScan" value="package.persistence.entity" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.Oracle12cDialect" />
</bean>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="EntityM" />
</bean>
The Excpetion is:
Context initialization failed: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springSessionJdbcOperations' defined in class path resource [org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfiguration.class]: Unsatisfied dependency expressed through method 'springSessionJdbcOperations' parameter 0: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: datas1,datas2; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: datas1,jndi2.
I can not have two datasources configured, The first datasource is to the entityManagar and second datasource is to call stored procedure.
The JdbcSessionConfiguration expects a bean of type 'DataSource' with name 'dataSource'.
If this cannot be found, spring tries to use a bean of type 'DataSource', ignoring the name. Because there are two beans of this type, the exception is thrown.
Solution :
Rename the DataSource bean, that you want to use with the EntityManager from
'datas1' to 'dataSource'.
Spring should than be able to create JdbcSessionConfiguration and you can use the 'datas2' for whatever you need.

Spring Bean initialization - Date passed as String via xml not working for step scope

I need to pass currentDate as String to my sendMetaStatsTask tasklet appended in subject .
Now, if I create a bean with scope="step" using following xml
<bean id="sendMetaStatsTask" class="org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter" scope="step">
<property name="targetObject">
<bean class="com.nextag.catalog.springbatch.tasklets.GenerateReportFromQueriesTasklet">
<property name="mailTo" value="#{jobParameters['MAIL_TO']}"/>
<property name="mailFrom" value="#{jobParameters['MAIL_FROM']?:'wizereporter#nextag.com'}"/>
<property name="mailSubject" value="#{jobParameters['PARTNER_DOMAIN']+' Affiliate Seller Report - '+ currentDate.toString()}"/>
</bean>
</property>
<property name="targetMethod" value="execute"/>
</bean>
<bean id="fastDateFormat" class="org.apache.commons.lang.time.FastDateFormat" factory-method="getInstance">
<constructor-arg value="dd/MM/yyyy"/>
</bean>
<bean id="currentDate" class="java.util.Date" factory-bean="fastDateFormat" factory-method="format" scope="step">
<constructor-arg>
<bean class="java.util.Date"/>
</constructor-arg>
</bean>
It throws :-
Error creating bean with name 'currentDate' defined in BeanDefinition
defined in file
[/home/nextag/Apache6/tomcat/webapps/nextag/WEB-INF/classes/META-INF/spring/batch/jobs/seller-meta-stats-logging-job.xml]:
Initialization of bean failed; nested exception is
java.lang.IllegalStateException: Cannot create scoped proxy for bean
'scopedTarget.currentDate': Target type could not be determined at the
time of proxy creation.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
However, it works fine in case I use prototype .
Need to ask why it is not working in step scope, am I missing something ?
You need to tell the bean currentDate scope proxy so any injection is only valid to scope step. A good explanation is here
<bean id="fastDateFormat" class="org.apache.commons.lang.time.FastDateFormat" factory-method="getInstance">
<constructor-arg value="dd/MM/yyyy"/>
</bean>
<bean id="currentDate" class="java.util.Date" factory-bean="fastDateFormat" factory-method="format" scope="step">
<aop:scoped-proxy/>
<constructor-arg>
<bean class="java.util.Date"/>
</constructor-arg>
</bean>

Error creating bean with name defined in class path resource [application-context.xml] in spring framework

<bean id="MyDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg value="MyDataSource"/>
</bean>
Error creating bean with name 'template' defined in class path
resource [application-context.xml]: Could not resolve matching
constructor (hint: specify index/type/name arguments for simple
parameters to avoid type ambiguities)
I am not sure what I am doing wrong here to get above error? Have all of it defined in properties file correctly with correct variable name. what are the things to check for ?
change
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg value="MyDataSource"/>
</bean>
to
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="MyDataSource"/>
</bean>
because you don't want to inject String value you want to inject referred bean
I got the same error and in my bean class, I declared the default constructor which was not declared earlier and it solved my problem.
So My complete error is like
Error creating bean with name 'student1' defined in class path resource [config.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.springcore.springcore.Student]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.springcore.springcore.Student.()

Categories