Spring + JPA LocalContainerEntityManagerFactoryBean. packagesToScan regex support? - spring

I want configure my EntityManagerFactoryBean property "packagesToScan" to use a regex (like spring context component-scan does normally). But the most similar behavior that I can get is with an array of packages.
There are a version like this for use?
com.myproject.*.entity
this my EntityManagerFactoryBean config:
<beans:bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="packagesToScan">
<beans:array>
<beans:value>com.myproject.web.users.entity</beans:value>
</beans:array>
</beans:property>
<beans:property name="jpaVendorAdapter">
<beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</beans:property>
<beans:property name="jpaProperties">
<beans:props>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop><!-- https://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do -->
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</beans:prop>
</beans:props>
</beans:property>
</beans:bean>

Related

I need to save the messaged in rabbit mq and delete it once my job is completed successfully

I need to save m messages in rabbit mq. I am using acknowledgeMode as MANUAL in SimpleMessageListenerContainer. This is helping me store the value in unacked in rabbit mq. But even after job completion the messages remain in the unacked. I need the messages to be deleted once job gets completed successfully. Please help me find a solution
<beans:bean id="PartitionHandler" class="org.springframework.batch.integration.partition.MessageChannelPartitionHandler" init-method="afterPropertiesSet" scope="job">
<beans:property name="messagingOperations" ref="messagingTemplate"></beans:property>
<beans:property name="stepName" value="slave" />
<beans:property name="gridSize" value="${spring.gridsize}" />
<beans:property name="pollInterval" value="5000"></beans:property>
<beans:property name="jobExplorer" ref="jobExplorer"></beans:property>
<beans:property name="replyChannel" ref="outboundReplies"></beans:property>
</beans:bean>
<beans:bean id="PeriodicTrigger" class="org.springframework.scheduling.support.PeriodicTrigger">
<beans:constructor-arg value="5000"></beans:constructor-arg>
</beans:bean>
<beans:bean id="requestQueue" class="org.springframework.amqp.core.Queue">
<beans:constructor-arg name="name" value="testQueue">
</beans:constructor-arg>
<beans:constructor-arg name="durable" value="true">
</beans:constructor-arg>
</beans:bean>
<int:poller id="PollerMetadata" default="true" trigger="PeriodicTrigger" task-executor="taskExecutor"></int:poller>
<beans:bean id="amqptemplate"
class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<beans:property name="connectionFactory" ref="rabbitConnFactory" />
<beans:property name="routingKey" value="testQueue"/>
<beans:property name="queue" value="testQueue"/>
</beans:bean>
<beans:bean id="amqpOutboundEndpoint" class="org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint">
<beans:constructor-arg ref="amqptemplate"/>
<beans:property name="expectReply" value="false"></beans:property>
<beans:property name="routingKey" value="testQueue"></beans:property>
<beans:property name="outputChannel" ref="inboundRequests"></beans:property>
</beans:bean>
<int:service-activator ref="amqpOutboundEndpoint" input-channel="outboundRequests"/>
<beans:bean id="SimpleMessageListenerContainer" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<beans:constructor-arg ref="rabbitConnFactory"/>
<beans:property name="queueNames" value="testQueue"></beans:property>
<beans:property name="autoStartup" value="false"></beans:property>
<beans:property name="acknowledgeMode" value="MANUAL"></beans:property>
<beans:property name="concurrentConsumers" value="5"></beans:property>
</beans:bean>
<beans:bean id="AmqpInboundChannelAdapter" class="org.springframework.integration.amqp.inbound.AmqpInboundChannelAdapter" init-method="afterPropertiesSet">
<beans:constructor-arg ref="SimpleMessageListenerContainer"/>
<beans:property name="outputChannel" ref="inboundRequests"></beans:property>
</beans:bean>
<beans:bean id="StepExecutionRequestHandler" class="org.springframework.batch.integration.partition.StepExecutionRequestHandler">
<beans:property name="jobExplorer" ref="jobExplorer"/>
<beans:property name="stepLocator" ref="stepLocator"/>
</beans:bean>
<int:service-activator ref="StepExecutionRequestHandler" input-channel="inboundRequests" output-channel="outboundStaging"/>
<bean id="rabbitConnFactory"
class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<constructor-arg><value>localhost</value></constructor-arg>
<property name="username" value="guest" />
<property name="password" value="guest" />
<property name="virtualHost" value="/" />
<property name="port" value="5672" />
</bean>
<bean id="admin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="rabbitConnFactory" />
</bean>
<bean id="messagingTemplate"
class="org.springframework.integration.core.MessagingTemplate">
<constructor-arg ref="outboundRequests" />
<property name="receiveTimeout" value="60000000"/>
</bean>
<bean id="outboundRequests" class="org.springframework.integration.channel.DirectChannel" >
<property name="maxSubscribers" value="5"></property>
</bean>
<int:channel id="outboundReplies" scope="job"><int:queue/></int:channel>
<bean id="outboundStaging" class="org.springframework.integration.channel.NullChannel"></bean>
<bean id="inboundRequests" class="org.springframework.integration.channel.QueueChannel"></bean>
<bean id="stepLocator" class="org.springframework.batch.integration.partition.BeanFactoryStepLocator"/>
When using MANUAL acks, you are responsible for the acknowledgment.
See my answer to this question.

FormatterLineAggregator in spring

I am using FormatterLineAggregator. For eg:
<beans:property name="lineAggregator">
<beans:bean class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
<beans:property name="fieldExtractor">
<beans:bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<beans:property name="names" value="desc1, desc2, desc3, desc4, desc5" />
</beans:bean>
</beans:property>
<beans:property name="format" value="%-4s%-9s%-30s%-30s%-30s" />
</beans:bean>
</beans:property>
The desc2 is of size 9. But I am getting a value of size 4 from database. So in the output file, spaces appear for the remaining 5 characters. I want to remove this space.Even if the size is 9 spaces should not follow desc1.

Why isn't hibernate creating my tables from hbm.xmls?

I have several .hbm.xml mapping files set up in Spring, and I can tell from the logs that Spring sees them and does something with them, but no tables are created in the DB.
Bean setup:
<beans:bean id="dataSourceEmbedded" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="org.h2.Driver" />
<beans:property name="url" value="jdbc:h2:~/myDbName" />
<beans:property name="username" value="sa" />
<beans:property name="password" value="" />
</beans:bean>
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSourceEmbedded">
<beans:property name="mappingLocations">
<beans:list>
<beans:value>classpath:path/to/schemas/Page.hbm.xml</beans:value>
<beans:value>classpath:path/to/schemas/Navigation.hbm.xml</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:value>
hibernate.show_sql=true
hibernate.dialect=${hibernate.dialect}
hibernate.hbmToDdlAuto=create
</beans:value>
</beans:property>
</beans:bean>
I know that the path to the Page and Navigation mapping files is correct - an error is thrown if it's not, and I can see the fields being mapped during the tomcat startup:
DEBUG: org.hibernate.cfg.Configuration - Processing hbm.xml files
INFO : org.hibernate.cfg.HbmBinder - Mapping class: Page -> pages
DEBUG: org.hibernate.cfg.HbmBinder - Mapped property: Page ID -> pageid
DEBUG: org.hibernate.cfg.HbmBinder - Mapped property: title -> title
Similarly, the database is clearly accessed / locked while tomcat is loading, so something is happening in there! But once it's loaded and I try to query it or inspect it, no tables exist.
Question: Why aren't my tables being created / what have I done wrong?
The property you used is wrong, it should be 'hibernate.hbm2ddl.auto' instead of 'hibernate.hbmToDdlAuto'... However instead of that you can also set the schemaUpdate property of the LocalSessionFactoryBean.
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSourceEmbedded">
<beans:property name="mappingLocations">
<beans:list>
<beans:value>classpath:path/to/schemas/Page.hbm.xml</beans:value>
<beans:value>classpath:path/to/schemas/Navigation.hbm.xml</beans:value>
</beans:list>
</beans:property>
<beans:property name="schemaUpdate" value="true" />
<beans:property name="hibernateProperties">
<beans:value>
hibernate.show_sql=true
hibernate.dialect=${hibernate.dialect}
</beans:value>
</beans:property>
</beans:bean>
Related answer/info: Hibernate hbm2ddl.auto possible values and what they do?
Correct property name is hibernate.hbm2ddl.auto

What this running error means?

Hello, I`m trying to run a job in spring batch but its giving me this error which i dont know how to solve:
`$` C:\Program Files\Microsoft Visual Studio 8\VC>java -jar C:\Workspacetest\testpro
ject\target\ams-eod-0.0.1-SNAPSHOT.jar copyJob 1 `$`
Exception in thread "main" java.lang.NullPointerException
at sun.launcher.LauncherHelper.getMainClassFromJar(LauncherHelper.java:399)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:463)
However this is my applicationcontext. can anyone help me regarding this error please?
few words to discribe what the job does, this job reads from a database some records and then it performs a calculation on each record (row * 3.75) and then update the record.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:beans="http://www.springframework.org/schema/beans"
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/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd">
<beans:bean id="itemReader" class="org.spr...JdbcCursorItemReader">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="sql" value="select * from Trans WHERE id =?"/>
<beans:property name="rowMapper" ref= "CustomerCreditRowMapper"/>
<beans:property name="preparedStatementSetter" ref="idSetter"/>
</beans:bean>
<beans:bean id="transactionProcessor" class="">
<beans:property name="targetObject" ref="Processing"/>
<beans:property name="targetMethod" value="process"/>
</beans:bean>
<beans:bean id="jdbcWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="sql" value="update Trans set (amount) values (?)"/>
<beans:property name="preparedStatementSetter" ref="idSetter"/>
</beans:bean>
<step id="CopyFileStep">
<tasklet>
<chunk reader="itemReader"
processor="transactionProcessor"
writer="jdbcWriter"
commit-interval="10"/>
</tasklet>
</step>
<job id="copyJob">
<step id="step1" parent="CopyFileStep"/>
</job>
<beans:bean id="preparedStatementSetter"
class="sa.com.anb.itg.dev.settlement.batch.Itemwriter"/>
<beans:bean id="idSetter" class="sa.com.anb.itg.dev.settlement.batch.idSetter"
scope="step">
<beans:property name="id" value="#{jobParameters[id]}"/>
</beans:bean>
<beans:bean id="CustomerCreditRowMapper"
class="sa.com.anb.itg.dev.settlement.batch.CustomerCreditRowMapper"/>
<beans:bean id="AmountToTransactionItemProcessor"
class="sa.com.anb.itg.dev.settlement.batch.AmountToTransactionItemProcessor">
<beans:property name="TransactionDAO" ref="TransactionDAO"/>
</beans:bean>
<beans:bean id="TransactionDAO" class="sa.com.anb.itg.dev.settlement.batch.TransactionDAO">
<beans:property name="datasource" ref="dataSource"/>
</beans:bean>
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<beans:property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<beans:property name="url" value="jdbc:oracle:thin:#192.168.84.208:1556:pay10g" />
<beans:property name="username" value="ams_recon" />
<beans:property name="password" value="ams_recon" />
</beans:bean>
<beans:bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="transactionManager" ref="transactionManager"/>
</beans:bean>
<beans:bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<beans:property name="jobRepository" ref="jobRepository" />
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager" lazy-init="true">
<beans:property name="dataSource" ref="dataSource"/>
</beans:bean>
</beans:beans>
Well, when launching an App you REALLY need a Main Class.
Spring Batch can provide one for you : CommandLineJobRunner.
In your example it seems that your are trying to execute your app like a regular one with the commandLine. Here Java try to locate you Manifest File because the JVM NEEDS a Main class and it's not able to find it in your JAR which cause your error.
It's not your app that you have to launch with the command line, it's the CommandLineJobRunner which provide a Main method able to launch your batch.
The documentation show how to use it : http://static.springsource.org/spring-batch/reference/html/configureJob.html#runningJobsFromCommandLine

Spring security SessionRegistry and bean based configuration woes

I'm using Spring Security 3.0.5 and trying to get a count of currently logged in users. My scenario is Pre-Authenticated and using bean based configuration as opposed to <http> namespace based configuration (in which case this appears to be trivial.
My config file is as follows:
<beans:bean id="springSecurityFilterChain"
class="org.springframework.security.web.FilterChainProxy">
<filter-chain-map path-type="ant">
<filter-chain pattern="/**/resources/**" filters="none" />
<filter-chain pattern="/**/logout/**" filters="none" />
<filter-chain pattern="/service/**" filters="none" />
<filter-chain pattern="/**"
filters="sif,concurrencyFilter,shibbolethFilter,smf,logoutFilter,etf,fsi" />
</filter-chain-map>
</beans:bean>
<beans:bean id="sif"
class="org.springframework.security.web.context.SecurityContextPersistenceFilter" />
<beans:bean id="scr"
class="org.springframework.security.web.context.HttpSessionSecurityContextRepository" />
<beans:bean id="smf"
class="org.springframework.security.web.session.SessionManagementFilter">
<beans:constructor-arg name="securityContextRepository"
ref="scr" />
<beans:property name="sessionAuthenticationStrategy"
ref="sas" />
</beans:bean>
<beans:bean id="shibbolethFilter"
class="PreAuthenticatedShibbolethAuthenticationFilter">
<beans:property name="authenticationManager" ref="authenticationManager" />
<beans:property name="exceptionIfHeaderMissing" value="true" />
<beans:property name="continueFilterChainOnUnsuccessfulAuthentication"
value="true" />
<beans:property name="developmentMode" value="true" />
<beans:property name="authenticationSuccessHandler"
ref="customAuthenticationSuccessHandlerBean" />
</beans:bean>
<beans:bean id="sas"
class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
<beans:constructor-arg name="sessionRegistry"
ref="sessionRegistry" />
<beans:property name="maximumSessions" value="1" />
</beans:bean>
<beans:bean id="sessionRegistry"
class="org.springframework.security.core.session.SessionRegistryImpl" />
<beans:bean id="concurrencyFilter"
class="org.springframework.security.web.session.ConcurrentSessionFilter">
<beans:property name="sessionRegistry" ref="sessionRegistry" />
<beans:property name="expiredUrl" value="/session-expired.html" />
</beans:bean>
<authentication-manager alias="authenticationManager">
<authentication-provider ref='preauthAuthProvider' />
</authentication-manager>
<beans:bean id="preauthAuthProvider"
class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
<beans:property name="preAuthenticatedUserDetailsService">
<beans:bean id="userDetailsServiceWrapper"
class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
<beans:property name="userDetailsService" ref="userDetailsService" />
</beans:bean>
</beans:property>
</beans:bean>
<beans:bean id="logoutHandlerBean"
class="LogoutSuccessHandlerImpl" />
<beans:bean id="userDetailsService"
class="CustomJdbcDaoImpl">
<beans:property name="dataSource" ref="projectDS" />
<beans:property name="enableGroups" value="true" />
<beans:property name="enableAuthorities" value="false" />
</beans:bean>
In my controller I have the following code:
#Resource(name="sessionRegistry")
private SessionRegistry sessionReg;
private void doTest() {
List<Object> principals = sessionReg.getAllPrincipals();
for (Object o : principals) {
List<SessionInformation> siList = sessionReg.getAllSessions(o,
true);
for (SessionInformation si : siList) {
logger.error(si.getSessionId() + " " + si.getPrincipal());
}
}
}
The list principals is always empty. I feel the PreAuthenticatedShibbolethAuthenticationFilter filter which extends AbstractPreAuthenticatedProcessingFilter should get a ref to ConcurrentSessionControlStrategy, however, there is no such property which could be set.
What am I missing?
SecurityContextPersistenceFilter requires a SecurityContextRespository
<bean id="sif" class="org.springframework.security.web.context.SecurityContextPersistenceFilter" >
<property name="securityContextRepository" ref="scr" />
</bean>

Resources