JTraining Blog

Share your knowledge.
Tags >> Frameworks
Casyi
Casyi

Coming to your area: the 2010 Red Hat Road Tour. What makes this tour interesting? It's a half-day professional business conference about IT infrastructure: focusing on cloud computing, virtualization, middleware and the future of IT. These subjects will be covered by Red Hat cloud experts.

Red Hat says that their vision of IT is different in a positive way than of any other IT vendor. They know that IT infrastructure exists of many different pieces of hardware and software vendors that must cooperate with each other. They say that they recognize IT businesses want to grow and improve their IT systems and operations gradually and not through wrenching change. And they know that you want to do so in a way that preserves your strategic flexibility and keeps your options open.

They are going to visit areas in DC or Atlanta. Below you can see which cities on what date will be visited in the tour:


Tagged in: Tutorial , Tour , Source , software , Road , Release , related , Red , programming , programmer , Open , jtraining.com , JTraining , jQuery , Java , Hat , Groovy , GlassFish , Frameworks , Framework , Events , development , developer , DC , Computing , code , Cloud , beginner , Atlanta , Agile , advanced , 2010
Casyi
Casyi
If you don't know IntelliJ IDEA yet, I can tell you it's worth a try especially with the new release coming up.

IntelliJ IDEA, often referred to as 'IDEA', is a Java IDE by JetBrains. What distinguishes this IDE from others is that this one is focused on developer productivity. The official JetBrains site says:

 "It's the most intelligent Java IDE. IntelliJ IDEA deeply understands your code and gives you a set of powerful tools without imposing any particular workflow or project structure. IntelliJ IDEA is your dream pair-programmer who knows its way around the codebase, makes great suggestions right when you need them, and is always ready to help you shape your code."
 
This promises a lot right? A while ago JetBrains annouced its first Early Access release of IntelliJ IDEA X (10). The improvements will be focused on Flex and Groovy/Grails developers. Also Spring and Maven support will receive some new features.

The idea of having an Early Access program is to listen to the developers, so we all can give our opinions about it and make the IDE even better. This is what JetBrains site says:

"We at JetBrains believe that making tools for developers should greatly involve listening to developers. Our Early Access Program lets development community closely participate in discussions devoted to IntelliJ IDEA and influence development planning, from early stages onwards.
 Early Access Program allows you to try pre-release versions of our software to evaluate features that will be added to the next release." 
 
If JetBrains doesn't convince you, I suggest you read the blog by Grazer about the things he loves about IntelliJ IDEA, here. Some of the subjects he covers are the Smart Intentional Programming Support, Keyboard Navigation of Search Results, Fast Line Copy & Cut, Buffer History and more cool features of IDEA.

Convinced? Download yourself a 30-day fully functional trial of the IntelliJ IDEA commercial edition. There is also a free open-source Community Edition available for download.

To check out the Early Access Program releases (build 96.1020 was released on Aug 25, 2010), click here.

For more about the upcoming new features, read this article

The official site can be found here.

Tagged in: Tutorial , Source , software , Release , related , programming , programmer , Open , jtraining.com , JTraining , jQuery , JetBrains , Java , IntelliJ , IDEA , Groovy , GlassFish , Frameworks , Framework , Events , Early , development , developer , Computing , code , Cloud , beginner , Agile , advanced , Access Program
sneake75@hotmail.com
sneake75@hotmail.com Introduction:

In part 1 we talked about Caching introduction and some terminologies of caching and in part 2 and part 3 we have seen some implementation of the famous replacement cache algorithms and in part 4 we saw comparisons between some famous caching frameworks and in this part we are going to continue what we started in part 4 and as in part 4 we will concentrate only on memory caching.


The Task:

After programmer 1 released the caching article in “Programming Mania” the geek to geek magazine, he got a lot of threaten mails and terrible messages from caching geeks defending their beloved caching frameworks and warning him if he didn’t make their beloved caching framework win the contest, he will regret the day he became a programmer.

That didn’t scare our programmer and he went on completing the second part of the comparison

. ShiftOne
· WhirlyCache
· SwarmCache
· JBoss Cache

ShiftOne:

ShiftOne or as they call JOCache is a lightweight caching framework that implements several strict object caching policies which comes up with a set of cache algorithm implementations that supports in memory cache.

ShiftOne cache forces two rules for every cache:

  • Max Size - each cache has a hard limit on the number of elements it will contain. When this limit is exceeded, the least valuable element is evicted. This happens immediately, on the same thread. This prevents the cache from growing uncontrollably
  • Element Timeout - each cache has a maximum time that it's elements are considered valid. No element will ever be returned that exceeds this time limit. This ensures a predictable data freshness.

ShiftOne use decorator pattern in order to make it more flexible for the user to use any underneath caching product to maintain the cache.

















Tagged in: Frameworks , Caching , Algorithms
sneake75@hotmail.com
sneake75@hotmail.com

Introduction:

In part 1 we talked about Caching introduction and some terminologies of caching and in part 2 and part 3 we have seen some implementation of the famous replacement cache algorithms and now in this part we will see comparison between open source java caching frameworks as I am not that rich to buy commercial frameworks :D.
In this part we will talking about OSCache,Ehcache,JCS and Cache4J and we are going to concentrate on memory caching only, there will be performance comparison based on in memory caching by using JBoss caching benchmark framework and other test cases for cache.

The Task:

“Programming Mania” is a famous programming magazine from geeks to geeks every release from the magazine there a section specialized in frameworks comparison like MVC, ORM and so on, this month they decided that they are going to make a comparison about caching frameworks

And as we know the editors have programmatic background, in fact they are real programmers (not fake ones).



Tagged in: Frameworks , Caching , Cache , Algorithms
sneake75@hotmail.com
sneake75@hotmail.com

Introduction:

In part 1 we talked about the basics and terminologies of cache and we have also shown replacement policies , in part 2 we implemented some of these famous replacement polices and now in this part we will continue talking about the implementation of two famous algorithms which are LFU and LRU. Again, the implementation in this article is for sake of demonstration and in order to use it (we just concentrate over the replacement algorithm and we will skip other things like loading data and so on), you will have to do some extra work but you can base your implementation over it.

Meet LFU Cache Implementation:

public synchronized Object getElement(Object key) {

Object obj;

obj = table.get(key);

if (obj != null) {
CacheElement element = (CacheElement) obj;
element.setHitCount(element.getHitCount() + 1);
return element.getObjectValue();
}
return null;

}

public final synchronized void addElement(Object key, Object value) {

Object obj;

obj = table.get(key);

if (obj != null) {
CacheElement element;

// Just replace the value.
element = (CacheElement) obj;
element.setObjectValue(value);
element.setObjectKey(key);

return;
}

if (!isFull()) {

index = numEntries;
++numEntries;
} else {
CacheElement element = removeLfuElement();
index = element.getIndex();
table.remove(element.getObjectKey());

}

cache[index].setObjectValue(value);
cache[index].setObjectKey(key);
cache[index].setIndex(index);
table.put(key, cache[index]);
}

public CacheElement removeLfuElement() {

CacheElement[] elements = getElementsFromTable();
CacheElement leastElement = leastHit(elements);
return leastElement;
}

public static CacheElement leastHit(CacheElement[] elements) {

CacheElement lowestElement = null;
for (int i = 0; i < elements.length; i++) {
CacheElement element = elements[i];
if (lowestElement == null) {
lowestElement = element;

} else {
if (element.getHitCount() < lowestElement.getHitCount()) {
lowestElement = element;
}
}
}
return lowestElement;
}
Analyzing LFU Cache Code (Talk Show):


Presenter: it is getting hotter and hotter now, our next contestant is LFU cache, please make some noise for it.

Audience began to scream for LFU which made LFU hesitated.

Hello, I am LFU, when the cache client want to add a new element and cache is full (no enough room for the new entry) I will have to kick out the least frequently used entry, by using the help of the removelfuElement method which will allow me to get the least frequently used element, after I get it, I will remove this entry and place the new entry


else {
CacheElement element = removeLfuElement();
index = element.getIndex();
table.remove(element.getObjectKey());

}


If we dived into this method…, I am saying if we dived into this method (still nothing happened)
LFU tried pressing the next button on the presentation remote control (to get the next presentation slide) but I didn’t work.

Ahh now we are talking, ok if we dived into this method we will see that the method is just getting the whole elements in cache by calling getElementsFromTable method and then returns the element with the least hit.


public CacheElement removeLfuElement() {

CacheElement[] elements = getElementsFromTable();
CacheElement leastElement = leastHit(elements);
return leastElement;
}
}


By calling leastHit method which loops over the cache elements and check if the current element has the least hit, if so, I will make it my lowestElement which I am going replace the new entry with.

public static CacheElement leastHit(CacheElement[] elements) {

CacheElement lowestElement = null;
for (int i = 0; i <>
CacheElement element = elements[i];
if (lowestElement == null)
{ lowestElement = element; }
else {

if (element.getHitCount() <>
{ lowestElement = element; }
}
}
return lowestElement;
}

LFU stopped talking and waited for any action from the audience and the only action it get was scratching heads (audience didn’t get some stuff).

One of the production team whispered to LFU cache and said: you didn’t mention how the lowest element will be distinguished from another element?

Then LFU cache started talking gain and said: By default when you add the element to the cache its hitCoint will be the same as the previous element so how do we handle the hit count thing?

Every time I encounter a cache hit I will increment the hit count of the entry and then return the entry the cache client asked for which would be something like that


public synchronized Object getElement(Object key) {

Object obj;

obj = table.get(key);

if (obj != null) {
CacheElement element = (CacheElement) obj;
element.setHitCount(element.getHitCount() + 1);
return element.getObjectValue();
}
return null;

}


Magnifying the Code:

Did anyone say magnification?

 























































































































































Tagged in: Frameworks , Cache , Algorithms
evdh
evdh

Audit is a process that can apply many fields of an enterprise including information systems, says Whether it is internal (Audit Department) or external (Outsourced), audit is important to control people, infrastructure and third party relations. Many business processes are carried out by transaction processing systems with a database backend. To track and spot the processes, we need a change history for every transaction. In enterprise applications, this is possible with data change logging.

Ibrahim  identifies 2 techniques.

  • Audit Trailing on the table (using metacolumns to store this information)
Pros:
Easy to record trail data
Audit data size is small
Easy to fetch trail data with SQL statements

Cons:
Only one update trail data can be stored
Can’t store changed data
Data and row meta-data(trail data) is merged
  • Audit Trailing on an external table
Pros:
Insert and every update can be logged
Changed data can be logged
Separated form the transactional data
By adding a transaction id to logs (Log table has a transaction id column), it is possible to see every change in a transaction (This is a shining feature when debugging transaction boundaries).

Cons:
Hard to fetch trail data with SQL
Trail data is bigger since PK columns and table names are also stored within every row.
The decision was to use the second technique in their ORM framework so auditing would not need any developer writing log or SQL code. Also, logging occurs asynchronously so the application doesn't need to wait for the auditing process to finish.

Tagged in: Frameworks , Audit
farmerzen
farmerzen

I've been using Itext for a while now. It's a great framework for generating PDF's. It's quite simple to use and you can produce fancy PDF's with it.

Usually I've been using it in a web context, for producing reports of some kind.

The project home page is http://www.lowagie.com/iText/, it's very well documented, it has lot's of examples and it has a great mailling list.

The only draw back is if you want to do something too complex you will have to buy the book or do a lot of research in the mailing lists. In the book you will find the techniques for doing it right  and also you will support the framework author (Bruno Lowagie).

Ok, first of all, check out the download on the web site. Add the JAR to you lib or classpath and  read some examples at the kick start "learn by example" section.

My focus here is not to teach you how to build PDF's but some techniques and code samples for doing tasks that I struggle for a solution. And remember, the code showed here is just an example.

So let's go:

 

  • Create a header/footer with some images and page numbers like "X page of Y" and add it to all the pages of your document.


Well for doing this you have in the API the PdfPageEvent iterface or PdfPageEventHelper object that you can implement or extend. For me it's best to extend the PdfPageEventHelper and just override the desired methods.  Check the javadocs! Instanciate your object with the disared arguments (logo header, resorce bundle, etc...) that you will need.

writer.setPageEvent(new MyPdfPageEvent(resources, headerLeft, headerRight, false));

Now comes the tricky part: How do I create the total page for every page if the pdf is not rendered yet? For that, Itext have a Template objec. You need to instanciate it in onOpenDocument method somehow like this:

public void onOpenDocument(PdfWriter writer, Document document) {
            total = writer.getDirectContent().createTemplate(100, 100);
            total.setBoundingBox(new Rectangle(-20, -20, 100, 100)); }

Now we want to add the "page X of Y" string to every page:

public void onEndPage(PdfWriter writer, Document document) {
        String text = resources.getString("pei.export.pdf.footer.page.number") + " " + writer.getPageNumber() + " " +  resources.getString("pei.export.pdf.footer.page.of") + " ";

        float footerBase = document.bottom() - 30;

        float textSize = 8;
        float adjust = 2;

        PdfContentByte cb = writer.getDirectContent();
       cb.beginText();
       cb.setFontAndSize(helv, 6);
       cb.setTextMatrix(document.right() - textSize - adjust, footerBase);
       cb.showText(text);
       cb.endText();
       cb.addTemplate(total, document.right() - adjust, footerBase);
      //important to control pdf errors
      cb.sanityCheck();    }

And now for finishing you need to add the total pages template after render the document like this:
    public void onCloseDocument(PdfWriter writer, Document document) {
        total.beginText();
        total.setFontAndSize(helv, 6);
        total.setTextMatrix(0, 0);
        total.showText(String.valueOf(writer.getPageNumber() - 1));
        total.endText(); }

 Easy but triky... You can try to add headers logo also onOpenDocument method showed above like this:

     headerRight= Image.getInstance(logoPath);
    cb.addImage(headerRight, 150, 0, 0, headerRight.getHeight() / headerRight.getWidth() * 150, document.right() - 150, headerBase); 
 

The PageEventHelper class also have other methods for control other document events like onStartPage, onChapter, onChapterEnd etc... Do it your way!






























Tagged in: Frameworks
farmerzen
farmerzen

From my last project I used jackrabbit for building a content management system.

One of the reasons I choose it insted of a relational database, where the features that it supports.

Indexing and searching is very nice (it uses Lucene). You can add weights to your searchable attributes. It supports clustering and caching also! Etc... it has lot's of features! And the great thing is that it supports container transactions also. :)


Tagged in: Frameworks
sneake75@hotmail.com
sneake75@hotmail.com

Introduction:

A lot of us heard the word cache and when you ask them about caching they give you a perfect answer but they don’t know how it is built, or on which criteria I should favor this caching framework over that one and so on, in this article we are going to talk about Caching, Caching Algorithms and caching frameworks and which is better than the other.

The Interview:


Tagged in: Performance , Java SE , Java EE , Frameworks , Caching , Algorithms

Tags:

Sponsers