Within a controller:
def query = entityManager.createQuery("select from Account a where a.key=:key order by ${params.sort} ${params.order}")
query.setParameter("key",session.key)
query.setMaxResults( params.max )
query.setFirstResult( params.offset )
def accounts = query.resultList;
EntityManager isn't injected in Services but jpaTemplate is so:
private getEntityManager(){
jpaTemplate.execute({EntityManager entityManager->
return entityManager;
} as JpaCallback);
}
Tuesday, August 31, 2010
Thursday, July 22, 2010
Android Development
I know i've been neglecting this blog for a while, but i've recently picked up a Samsung Galaxy S and will be refocusing on developing apps on Android and posting what i learn here.
Friday, May 7, 2010
Resolving filename conflict
In case i forget
/*
*Returns a existing file if the filename given has already been used in a folder/root
**/
public FileResource getConflictedFile(String fileName, String licenseKey, Long parentDirectoryId){
return FileResource.withCriteria(uniqueResult:true){
ilike("name","%"+fileName+"%")
eq("licenseKey",licenseKey)
if(parentDirectoryId){
parentDirectory{
eq("id",parentDirectoryId)
}
}
else{
isNull("parentDirectory")
}
fetchMode("parentDirectory",FM.EAGER)
}
}
public String createUniqueFileNameInFolder(String fileName, String licenseKey, Long parentDirectoryId){
def file = getConflictedFile(fileName, licenseKey, parentDirectoryId)
if( file ){
def fName = file.name
int dotPos = fName.lastIndexOf(".")
def name = fName.substring(0,dotPos);
def fileExt = fName.substring(dotPos+1);
//check if there is a _numeric at the end of the file name
if( name.lastIndexOf("_")==-1 ){
fName = name+"_1."+fileExt;
}
else{
int usPos = name.lastIndexOf("_");
def copyNumber = name.substring( usPos+1 )
if( copyNumber.isLong() ){
copyNumber = copyNumber.toLong()+1
name = name.substring(0,usPos)
fName = name+"_"+copyNumber+"."+fileExt;
}
else{
fName = name+"_1."+fileExt;
}
}
//if yes then extract, increment, append and rerun again until no conflicts
return createUniqueFileNameInFolder(fName,licenseKey,parentDirectoryId)
}
else{
return fileName;
}
}
Friday, October 23, 2009
XWiki plugin on app engine
Using xwiki plugin in your grails application on app engine?
Then you'll need to delete core.jar from the deployment folder
‹app-name›/target/war/WEB-INF/lib
Then you'll need to delete core.jar from the deployment folder
‹app-name›/target/war/WEB-INF/lib
Wednesday, October 21, 2009
JVM Coldstart on Google App Engine
When your application sitting on app engine has been idling for a while, app engine tends to shutdown the jvm instance containing your app, presumably to conserve limited resources.
I find that a problem because getting the application initially started up can be a slow and expensive process.
But the problem can be solved with cron jobs that call a url in your application at set intervals, thereby keeping your jvm all nice and warmed up.
Check out the documentation on the site.
All you've got to do is:
create cron.xml and place it in your WEB-INF folder
I find that a problem because getting the application initially started up can be a slow and expensive process.
But the problem can be solved with cron jobs that call a url in your application at set intervals, thereby keeping your jvm all nice and warmed up.
Check out the documentation on the site.
All you've got to do is:
create cron.xml and place it in your WEB-INF folder
<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
<cron>
<url>/recache</url>
<description>Repopulate the cache every 2 minutes</description> <schedule>every 2 minutes</schedule>
</cron>
<cron>
<url>/weeklyreport</url>
<description>Mail out a weekly report</description>
<schedule>every monday 08:30</schedule>
<timezone>America/New_York</timezone>
</cron>
</cronentries>
Tuesday, October 20, 2009
Slow Progress and issues
Progress has been a little slow lately,
a couple of hiccups to address:
a couple of hiccups to address:
- Populating more than one cache during bootstrapping seems to overwrite the previous cache with the latter cache.
- Google's url based security configured via google.appengine.security.requireLogin seems to remove parameters from url to redirect to target url.
Wednesday, October 7, 2009
JCache in App Engine and Grails
Caching is a great way to improve performance in any application, the less hits to the db for common tasks the greater the performance gain.
And here's how to do so in Java on Google's App Engine and Grails.
Google exposes their memcache caching system via the JCache interface
first of all you may wanna start with the javadocs here.
Since CacheManager is a singleton you can save some code by exposing it as a bean.
in resources.groovy:
Now you will need to initialize the cache, this has to be done only once so i do it in Bootstrap.groovy
See below for a practical example of using the cache:
One small caveat, you can store a collection of objects in the cache but it will need to be a ArrayList, so:
will not work, you will have to do
And here's how to do so in Java on Google's App Engine and Grails.
Google exposes their memcache caching system via the JCache interface
first of all you may wanna start with the javadocs here.
Since CacheManager is a singleton you can save some code by exposing it as a bean.
in resources.groovy:
cacheManager(javax.cache.CacheManager){
it.factoryMethod = "getInstance"
}
Now you will need to initialize the cache, this has to be done only once so i do it in Bootstrap.groovy
class BootStrap {
def cacheManager;
def init = { servletContext ->
CacheFactory cacheFactory = cacheManager.getCacheFactory();
def cache = cacheFactory.createCache([:]);
cacheManager.registerCache("cache",cache);
}
}
See below for a practical example of using the cache:
class ForumController {
def entityManager;
def list = {
def cache = cacheManager.getCache("cache")
def forums;
if(cache.get("allForums")){
forums = cache.get("allForums");
}
else{
forums = //your code to get all forums
//please note the collect enclosure.
cache.put("allForums",forums.collect{it});
}
["forums":forums]
}
}
One small caveat, you can store a collection of objects in the cache but it will need to be a ArrayList, so:
cache.put("allForums",Forum.list())will not work, you will have to do
cache.put("allForums",Forum.list().collect{it})
Subscribe to:
Posts (Atom)