mercredi 27 novembre 2013

Event based communication between wicket components


when we have a page structured with multiple panels, we need some times to update a component into a one panel following an event into another different panel in the same page, so how can we fix this type of scenario?
Starting from version 1.5 Wicket offers an event-based infrastructure for inter-component
communication. The infrastructure is based on two simple interfaces (both in package org.
apache.wicket.event) : IEventSource and IEventSink.

FIRST sending an event:

The first interface must be implemented by those entities that want to broadcast en event while the
second interface must be implemented by those entities that want to receive a broadcast event.
The following entities already implement both these two interfaces (i.e. they can be either sender or
receiver): Component, Session, RequestCycle and Application.
IEventSource exposes a single method named send which takes in input three parameters:
  1. sink: an implementation of IEventSink that will be the receiver of the event.
  2. broadcast: a Broadcast enum which defines the broadcast method used to dispatch the event to the sink and to other entities such as sink children, sink containers, session object,application object and the current request cycle. It has four possible values:
    1. BREADTH: The event is sent first to the specified sink and then to all its children components following a breadth-first order.
    2. DEPTH: The event is sent to the specified sink only after it has been dispatched to all its children components following a depth-first order.
    3. BUBBLE: The event is sent first to the specified sink and then to its parent containers.
    4. EXACT: The event is sent only to the specified sink.
  3. payload: a generic object representing the data sent with the event.
Each broadcast mode has its own traversal order for Session, RequestCycle and Application.
The below example shows an event sending. This method can be used from any other component: First you need to create one class to encapsulate the notification and the AjaxRequestTarget and pass them using the events infrastructure.
 
private class Notification {
    private String message;
    private AjaxRequestTarget target;
    ... constructor, getters, setters...
}
 
 send(getSession(), Broadcast.BREADTH, new Notification(message, target));

NEXT Receiving the event : 

Interface IEventSink exposes callback method onEvent(IEvent<?> event) which is triggered
when a sink receives an event. The interface IEvent represents the received event and provides getter
methods to retrieve the event broadcast type, the source of the event and its payload. Typically the
received event is used checking the type of its payload object :
 
@Override
public void onEvent(IEvent event) {
    if (event.getPayload() instanceof Notification) {
        Notification notification = (Notification) event.getPayload();
        ... do whatever you want before updating the panel ...
        // Update the panel 
        notification.getTarget().add(this);
    }
}
 You find in this link a project named InterComponetsEventsExample provides a concrete example of sending an event to a component (named 'container in the middle') using all the available broadcast methods:

dimanche 20 octobre 2013

How to load huge amount of data with Hibernate?


loading huge amount of data like more than one million of row can make a big problem, OutOfMemory Java error for example, so what can we do on such situation? with my little experience i can suggest two solutions, the first is the use of Criteria class and the second one is the ScrollableResults class.

  1. Criteria class : 
    with Creteria we can make pagination solution, this means that we split the result into differents pages to make it  easy to load and process, see the code below  :
    Criteria criteria = session.createCriteria(HistoryRecord.class);
    Criteria criteria = session.createCriteria(HistoryRecord.class).addOrder(Order.asc("CreatedDate") );
    criteria.setMaxResults(10);
    criteria.setFirstResult(20);
    criteria.list();
    

    these code let us take only 10 objects from index 20, even we have one million persisted objects into data base,we can avoid OutofMemory error. these code is not completed because it is static solution that take only object between 20 and 30 index. To be able to progressively load pages, setFirstResult() parametre should be dynamic because it represent the first index of the page, also this code should be called on demand.
  2. ScrollableResults class if you need to read each object separatly from a huge amout of data base objects instead of a complet page, you can use ScrollableResults class that make a iterator allows moving around within the results. See the below code : 
    
    Query query = session.createQuery(HistoryRecord.class);
    ScrollableResults results = query.scroll();
    while (results.next()) {
    HistoryRecord obj = (HistoryRecord) results.get()
    // make some instructions ...
    }
    results.close();
    
    

Spring Bean Scopes Example

In Spring, bean scope is used to decide which type of bean instance should be return from Spring container back to the caller :
5 types of bean scopes supported :

  1. singleton – Return a single bean instance per Spring IoC container
  2. prototype – Return a new bean instance each time when requested
  3. request – Return a single bean instance per HTTP request. *
  4. session – Return a single bean instance per HTTP session. *
  5. globalSession – Return a single bean instance per global HTTP session. *
In most cases, you may only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton.
P.S * means only valid in the context of a web-aware Spring ApplicationContext
for more informations, these notes were taken from : http://www.mkyong.com/spring/spring-bean-scopes-examples/

lundi 22 juillet 2013

There are no resources that can be added or removed from the server

Some time would like to deploy an ear file to WAS (Web Application Server) as usual. But Eclipse fail to find your EAR and it show an alert like this :

 There are no resources that can be added or removed from the server   

the solution is that your project needs to have a Eclipse Dynamic Web Module facet. 

  1. right click on project and click properties
  2. Go to Project Facet and Select dynamic web module and click apply.
  3. go to tomcat and click add/remove

How to fix GC overhead limit exceeded in Eclipse


Eclipse will throw GC overhead limit exceeded error when it runs out of memory, normally while performing memory-consuming operations such as building workspace on big projects.

 An internal error occurred during: "Building workspace". GC overhead limit exceeded  
To fix this problem, you'll need to allocate more memory to your Eclipse instance. To do this, go to the Eclipse installation directory, and locate the eclipse.ini file.
To increase the memory allocation for your Eclipse instance, edit the number in the following lines accordingly.
 -Xms1024m   
 -Xmx2048m  
The number is the amount of memory, in Megabytes.
You can also increase the value of MaxPermSize, as the following:
 -XX:MaxPermSize=1024m  

Restart Eclipse for the changes to take effect.

lundi 8 juillet 2013

Comment afficher une Valeur double avec 2 chiffres aprés la Virgule ?


La classe java.text.DecimalFormat permet de formater une valeur numérique dans le format de son choix en utilisant un pattern dont les symboles principaux sont les suivants :
  • 0 permet de représenter un chiffre qui devra obligatoirement être présent, même s'il s'agit d'un zéro inutile.
  • # permet de représenter un chiffre en ignorant les zéros inutiles.
  • . (le point) permet de représenter le séparateur de la partie décimale.
  • , (la virgule) permet de représenter le séparateur des groupes (milliers, millions, etc.).

(vous pouvez vous reporter à la documentation de la classe DecimalFormat pour obtenir la liste complète de tous les symboles).
Format 0 0,02 0,8 12,9
# 0 0 1 13
### 0 0 1 13
0 0 0 1 13
000 000 000 001 013
#.## 0 0,02 0,8 12,9
0.## 0 0,02 0,8 12,9
0.00 0,00 0,02 0,80 12,90
#.00 ,00 ,02 ,80 12,90
#,##0.00 0,00 0,02 0,80 12,90

par exemple :
double ma_Valeur ;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(df.format(ma_Valeur));

mercredi 3 juillet 2013

Restore Deleted Files using Git


Sometime you can delete a file  ,by accident, and then you commit your changes . after a while you recognize that you need this file So what you can do ??
what I have tried to resolve such problem (there are several ways) is  this few steps :
  1. List all files that have been deleted from my git repository :
    git log --diff-filter=D --summary
    
    
  2. Search the commit where you deleted your file
  3. undo your commit to the searched one BUT don't lose your modifications by taping this cmd :
    git reset --soft HEAD~n   
    

    n: is the number of undo commit steps

  4. if you type  git status  you will re-view your deleted file so to get the file back type this cmd :
    git checkout -- [file name]
    
Hope this could helps someone :D