[Logo] Terracotta Discussion Forums (LEGACY READ-ONLY ARCHIVE)
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
Accessing Spring beans inside ehcache XML  XML
Forum Index -> Ehcache
Author Message
saratbeesa

journeyman

Joined: 11/18/2009 02:32:19
Messages: 14
Offline

Hi all,

We have implemented the Ehcache solution for caching within our product ( 1.6.2 version of it ), we manage to inject the cache manager of Ehcache entirely using the spring's application context XML which worked real great.

Now, we want to use the new 'Cache Writer' feature that is introduced in Ehcache 2.0 in our application but stuck with a configuration problem. We have a database at the backend which acts as a master copy of all our data and we want to use Ehcache with 'Write-through' mechanism to keep our cache in sync with Database.

Since the 'Write-through' implementation factory and the classes are to be defined in the ehcache XML itself, we're not able to access our DAO Api from within this writer Class as all our POJOS are defined in a Spring configuration XML. For eg: We can't get a DB connection from the shared datasource as it is defined in the spring XML.

Briefly, we can not access Spring beans inside ehcache XML. Does Ehcache support this feature ?

Has anyone faced the similar problems before ?
Is there a work-around that we can use ?

Any pointers here would be of great help to us..

Sarat kumar.
alexsnaps

consul

Joined: 06/19/2009 09:06:00
Messages: 484
Offline

Hey,
Sadely you can't inject the cacheWriter bean entirely through configuration. But, the way that I solved this, using a "wiring bean" with an init-method that will do the wiring for you works.

So that:
Code:
   <bean id="someId" class="org.terracottatech.demos.SomeBean"
         init-method="init">
     <property name="cacheManager" ref="EhcacheManager"/>
     <property name="balanceWriterForCacheOne" ref="oneWriter"/>
     <property name="balanceWriterForCacheTwo" ref="twoWriter"/>
   </bean>
  


Where the init() method would programmatically wire the two cache writers injected by Spring to their matching caches...

Yet, no promise here, but I think that is something we should provide some support classes (if not more) for...
Hope that helps!

Alex Snaps (Terracotta engineer)
gluck

qaestor

Joined: 09/15/2009 18:01:23
Messages: 179
Location: Brisbane, Australia
Offline

CacheWriters should be able to be added programmatically to support IoC approaches. Have created a bug: https://jira.terracotta.org/jira/browse/EHC-662 to get this fixed.
saratbeesa

journeyman

Joined: 11/18/2009 02:32:19
Messages: 14
Offline

Thanks Alex,

Its an interesting solution though.. haven't thought in that direction at all.. I'll give it a try.

By the way, I came to learn that active mq also has a similar problem and they solved it by integrating with the spring XML itself, I mean, we can literally paste the active mq XML inside the spring XML and declare it as a configuration to the active MQ API.

Refer to section 'Using Spring 2.0' @ http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html

dinesh13

neo

Joined: 04/05/2011 03:22:46
Messages: 3
Offline

I was facing the same issue.
With the solution alex provided, it's working for 'Write Through'. But if I try the 'Write Behind' mode, its not working. It's giving null pointer exception. Any suggestions?
alexsnaps

consul

Joined: 06/19/2009 09:06:00
Messages: 484
Offline

Can you put the stacktrace here ?
I had this working for WriteBehind iirc, but without the exact issue, I won't be able to help...

Alex Snaps (Terracotta engineer)
dinesh13

neo

Joined: 04/05/2011 03:22:46
Messages: 3
Offline

Hi,
we have a persistence.xml where all data access components (commentRepository) are initialized.
When the cachewriter is invoked, the data access component initialized in spring config (persisntence.xml) is not available inside the cachewriter.
Here is the code snippets and stack trace.

persistence.xml
-----------------------------------------------------------------------------------
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>

<bean id="cacheWriterRegBean" class="com.test.abc.dao.CacheWriterRegBean" init-method="init">
<property name="cacheManager" ref="cacheManager"/>
<property name="commentCacheWriter" ref="commentCacheWriter"/>
</bean>
<bean id="commentCacheWriter" class="com.test.abc.dao.CommentCacheWriter">
<property name="commentRepository" ref="commentRepository"/>
</bean>

<bean id="commentRepository" class="com.test.abc.domain.repository.CommentRepositoryImpl">

</bean>

------------------------------------------------------------------------------------

Ehcache.xml
-------------------------------------------------------------------------------------
<cache name="CommentCache" maxElementsInMemory="5000" eternal="true"
overflowToDisk="false" memoryStoreEvictionPolicy="LFU" >

<cacheWriter writeMode="write-behind" notifyListenersOnException="true" minWriteDelay="1" maxWriteDelay="10"
rateLimitPerSecond="2000" writeCoalescing="false" writeBatching="true" writeBatchSize="500"
retryAttempts="2" retryAttemptDelaySeconds="1">
<cacheWriterFactory class="com.test.abc.dao.CommentCacheWriterFactory" />
</cacheWriter>
</cache>
-----------------------------------------------------------------------------------


CacheWriter.java
----------------------------------------------------------------------------------
@Autowired
CommentRepository<Comment,Long> commentRepository;
.........
public synchronized void write(Element element) {
try
{
if(!write) {
return;
}
Object value = element.getObjectValue();
Comment comment=(Comment)value;
System.out.println(comment);
commentRepository.create(comment);
}
catch(CacheException cex)
{
cex.printStackTrace();
}



}
...........

--------------------------------------------------------------------------------
CacheWriterRegBean.java

public class CacheWriterRegBean {

@Autowired
CacheManager cacheManager;
@Autowired
CacheWriter commentCacheWriter;

public void setCommentCacheWriter(CacheWriter commentCacheWriter) {
this.commentCacheWriter = commentCacheWriter;
}

public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}

public void init()
{
cacheManager.getCache("CommentCache").registerCacheWriter(commentCacheWriter);

}

--------------------------------------------------------------------------------

Ouput Stack Trace:
Apr 5, 2011 4:13:30 PM net.sf.ehcache.writer.writebehind.WriteBehindQueue processBatchedOperations
WARNING: Exception while processing write behind queue, retrying in 1 seconds, 2 retries left : null
Apr 5, 2011 4:13:31 PM net.sf.ehcache.writer.writebehind.WriteBehindQueue processBatchedOperations
WARNING: Exception while processing write behind queue, retrying in 1 seconds, 1 retries left : null
Exception in thread "CommentCache write-behind" java.lang.NullPointerException
at com.test.abc.dao.CommentCacheWriter.write(CommentCacheWriter.java:73)
at com.test.abc.dao.CommentCacheWriter.writeAll(CommentCacheWriter.java:86)
at net.sf.ehcache.writer.writebehind.operations.WriteAllOperation.performBatchOperation(WriteAllOperation.java:45)
at net.sf.ehcache.writer.writebehind.WriteBehindQueue.processBatchedOperations(WriteBehindQueue.java:363)
at net.sf.ehcache.writer.writebehind.WriteBehindQueue.processQuarantinedItems(WriteBehindQueue.java:329)
at net.sf.ehcache.writer.writebehind.WriteBehindQueue.processItems(WriteBehindQueue.java:274)
at net.sf.ehcache.writer.writebehind.WriteBehindQueue.access$200(WriteBehindQueue.java:47)
at net.sf.ehcache.writer.writebehind.WriteBehindQueue$ProcessingThread.run(WriteBehindQueue.java:147)
at java.lang.Thread.run(Thread.java:619)
alexsnaps

consul

Joined: 06/19/2009 09:06:00
Messages: 484
Offline

Unsure about these @Autowired annotations. Wouldn't it be related to that & the fact that the writer code is being executed on another thread now ? You do say it is working with this exact config if you switch to WriteThrough, right ?

Alex Snaps (Terracotta engineer)
dinesh13

neo

Joined: 04/05/2011 03:22:46
Messages: 3
Offline

Yes. With the exact same config, it's working fine for Write Through. The issue with CacheWriter in write behind must be because the it is executed in another thread.
 
Forum Index -> Ehcache
Go to:   
Powered by JForum 2.1.7 © JForum Team