View RSS Feed

My Java Tips

  1. ThreadLocal

    by , 10-29-2011 at 08:36 PM (My Java Tips)
    I will present an example that shows how to start, stop, suspend, and resume threads.


    It uses the Runnable interface because it extends from Applet class and multiple inheritance is not allowed. This example uses a thread that counts and prints a string every second. The thread starts when the applet is initialized. It continues to run until the user leaves the page. If the user returns to the page (and all is well), the thread continues from where it left off. This allows applets ...
    Categories
    Glassfish , Java SE
  2. The Thread Class

    by , 10-29-2011 at 08:29 PM (My Java Tips)
    java.lang.Thread is used to create and control threads. A new instance of this class must be created to use a new thread. Once a thread is created, it does not start running right away. Thread.start() must be called to actually make the thread run. On calling Thread.start(), the thread begins executing in the run() method of the target class.


    Remember that a new Thread class always starts running the public void run() method of a class. There are two ways to create a thread:
    ...

    Updated 10-29-2011 at 08:32 PM by Java Tip

    Categories
    Java ME , Java SE
  3. Java Threads - synchronized

    by , 10-28-2011 at 08:27 PM (My Java Tips)
    In multithreaded environment, data may be corrupted. A was to prevent this is to prevent the interruption of critical regions.


    You have to fidn the critical region in your code which you think should be accessed by one thread at a time. Java's synchronized is used to ensure that only one thread is in a critical region at once. When the thread enters a synchronized code block, it locks on that region and no other thread can enter the critical region as long as the lock is released. ...
    Categories
    Java SE
  4. View all keyboard shortcuts

    by , 10-28-2011 at 08:22 PM (My Java Tips)
    You need to know shortcut keys to speed up your work when working in Eclipse. In this post, Ill tell you how to view currently available key bindings.


    In Eclipse, just press Ctrl+Shift+L to see a full list of the currently available key bindings. This is a great way to learn what is available in the UI and to speed up your productivity by learning more key bindings. This information is also available in the improved General > Keys preference page.

    Name:  keycompletions.gif
Views: 233
Size:  12.3 KB ...
    Categories
    Uncategorized
  5. Eclipse - Key Binding Assistance

    by , 10-28-2011 at 08:20 PM (My Java Tips)
    Using keyboard shortcuts while working in Eclipse saves a lot of effort. But some shortcuts are not easy to remember and some comprise of multiple keys like: Alt+Shift+Q Y" ("Open Synchronize View").


    Eclipse provides a little pop-up showing you the possible completions for the keys you have pressed already. In the preferences, under General > Keys, there is an "Advanced" tab. Go to this tab, and check "Help Me With Multi-Stroke Keyboard Shortcuts". ...
    Categories
    Eclipse
  6. Eclipse - Creating path variables

    by , 10-28-2011 at 07:59 PM (My Java Tips)
    This post is about setting class path variables.


    When creating a linked folder or file, you can specify the target location relative to a path variable. By using path variables, you can share projects containing linked resources without requiring team members to have exactly the same path in the file system. You can define a path variable at the time you create a linked resource, or via the General > Workspace > Linked Resources preference page.

    Name:  path-vars.png
Views: 304
Size:  14.1 KB ...

    Updated 10-28-2011 at 08:04 PM by Java Tip

    Categories
    Eclipse
  7. GoogleTalk in Eclipse

    by , 10-27-2011 at 07:58 PM (My Java Tips)
    Eclipse communication Framework is a collection of communication and collaboration tools. One of exciting tool is support for talking to any Jabber server. This means that we can easily configure Google Talk to run inside an Eclipse view.


    You should have Eclipse Europa and ECF installed. To install ECF, go to Help -> Software Updates -> Find and Install > 'Search for new features to install'. The Europa Discovery Site (http://download.eclipse.org/releases/europa) should be ...
    Categories
    Eclipse
  8. Finally Block

    by , 10-25-2011 at 08:17 PM (My Java Tips)
    You must be using try catch block in your programming practices. The finally block is used to ensure resources are recovered regardless of any problems that may occur.


    Remember that there are several different ways of using the finally block and it depends on how exceptions are handled. Let me introduce the first style. If a method throws all exceptions, then it may use a finally with no catch :

    Java Code:
    import java.io.*;
     
    
    public final class SimpleFinally
    ...

    Updated 10-25-2011 at 08:36 PM by Java Tip

    Categories
    Java SE
  9. Eclipse - Quick Access

    by , 10-25-2011 at 08:11 PM (My Java Tips)
    Eclipse Europa introduces a noteworthy feature called Quick Access . Using it where needed can save time and money.



    The Quick Access is your window to; perspectives, views, open editors, commands, menus. After pressing Ctrl + 3, start typing, and it will show you the possible matches. It filters the most likely ones first, but if you press it again, will show all the possible matches that it knows about.

    Those of you who have used Ctrl+E to open up the list ...
    Categories
    Eclipse
  10. Enabling Debugging

    by , 10-25-2011 at 08:08 PM (My Java Tips)
    There are different ways to enable debugging in Glassfish. Let's start with enabling debugging using administration console.



    To enable debugging follow the following after each server restart:

    - Login to the Administration Console using web browser:
    http://host:port/asadmin
    For example:http://localhost:4848/asadmin

    - Go to the Application Server page.

    - Select the JVM Settings tab and the General option.
    ...
    Categories
    Glassfish
  11. Working with XML data

    by , 10-25-2011 at 07:59 PM (My Java Tips)
    Hibernate allows us to work with persistent XML data same as POJOs. A parsed XML tree is actually a representation of relational data at the object level, instead of POJOs.




    Hibernate uses dom4j API for manipulating XML trees. We can write queries that retrieve dom4j trees from the database and then all modifications to the tree automatically are synchronized to the database.

    One can take an XML document, parse it using dom4j, and write it to the database ...
    Categories
    Hibernate
  12. Hibernate - Fetching strategies

    by , 10-25-2011 at 07:49 PM (My Java Tips)
    Hibernate uses fetching strategy for retrieving associated objects when an application needs to navigate the association. Fetch strategies are declared in the object relational mapping metadata, or over-ridden by a particular HQL or Criteria query.




    Hibernate defines following fetching strategies:

    - Join fetching is used to retrieve the associated instance or collection in the same SELECT, using an OUTER JOIN.

    - Select fetching is used ...
    Categories
    Hibernate
  13. Hibernate Filters

    by , 10-21-2011 at 08:40 PM (My Java Tips)
    A Hibernate filter is a global, named, parameterized filter that may be enabled or disabled for a particular Hibernate session. Filters can be used like database views.




    Filters were introduced in Hibernate3. Using filters, you can define filter criteria and attach those filters at class and a collection level. A filter criteria is actually defining a restriction clause. This is same as "where" attribute available on the class and various collection elements. ...
    Categories
    Hibernate , XML
  14. Lazy associations

    by , 06-02-2011 at 08:26 PM (My Java Tips)
    Hibernate uses lazy select fetching by default for collections and lazy proxy fetching for single-valued associations. This default behavior makes sense for almost all associations in almost all applications.

    Hibernate will use the batch fetch optimization for lazy fetching if hibernate.default_batch_fetch_size parameter is set. This optimization may also be enabled at a more granular level.

    Lazy fetching introduces a problem that should be taken care of. Remember that ...
  15. Criteria instance

    by , 06-02-2011 at 08:22 PM (My Java Tips)
    Hibernate provides an interface org.hibernate.Criteria, which represents a query against a particular persistent class.

    Review the following example:

    Java Code:
    Criteria crit = session.createCriteria(Student.class);
    crit.setMaxResults(50);
    List students = crit.list();
    Resultset can be narrowed according to requirement. The class org.hibernate.criterion.Restrictions defines factory methods for obtaining certain built-in Criterion types. ...
    Categories
    Hibernate
  16. one-to-many, sql

    by , 06-02-2011 at 08:20 PM (My Java Tips)
    Let me presnet a simple <one-to-many> association from Parent to Child.

    XML Code:
    <set name="children">
    <key column="parent_id"/>
    <one-to-many class="Child"/>
    </set>
    Assume that we wish to execute the following code:

    Java Code:
    Parent p = .....;
    Child c = new Child();
    p.getChildren().add(c);
    session.save(c);
    session.flush();
    ...
    Tags: one-to-many Add / Edit Tags
    Categories
    Hibernate
  17. Svn revert

    by , 06-02-2011 at 08:16 PM (My Java Tips)
    You must have used SVN while working in a team on a Java project. In this post, Ill write about 'revert' command.

    Let me present a scenario. You made some changes in a class and now you realize that the changes are not required/not correct. You wish to discard your changes. You have to use svn revert to discard your changes.

    Java Code:
    $ svn revert foo.java
    Reverted foo.java
    Following switches are available:

    Java Code:
    --targets
    ...
    Tags: revert, svn Add / Edit Tags
    Categories
    Subversion
  18. No Persistence provider for EntityManager

    by , 06-02-2011 at 08:15 PM (My Java Tips)
    You might have encountered the following exception while working with hibernate:

    Java Code:
    [java] Exception in thread "main" javax.persistence.PersistenceException: 
    No Persistence provider for EntityManager named  ****
    Make sure you have all the required jars in the class path.

    Java Code:
          \antlr-2.7.6.jar
          \asm.jar
          \asm-attrs.jar
          \c3p0-0.9.0.jar
          \cglib-2.1.3.jar
    ...
  19. Static/Non static methods - Example

    by , 06-02-2011 at 08:12 PM (My Java Tips)
    I will present an example to show the difference between static and non-static methods.

    Go through the example below:

    Java Code:
    public abstract class AbstractExample {
    
       public static void foo() {
          System.out.println("Static foo called.");
       }
    
       public void bar() {
          System.out.println("Non-static bar called.");
       }
    }
    
    public class AbstractCaller extends AbstractExample {
    ...
    Categories
    Java SE
  20. Factory Methods

    by , 06-02-2011 at 08:11 PM (My Java Tips)
    This post lists some facts about factory methods.

    Factory methods are static methods that return an instance of the native class. Some examples from JDK include :

    • LogManager.getLogManager
    • Pattern.compile
    • Collections.unmodifiableCollection
    • Collections.synchronizeCollection
    • Calendar.getInstance


    Factory methods do not have names like constructors and do not need to create a new object upon each invocation. Objects can be cached and reused, if ...
  21. Memory Leaks

    by , 05-25-2011 at 11:04 AM (My Java Tips)
    You are aware of the fact that garbage collection collects and removes all the unwanted objects from memory without used intervention. But sometimes, you run into memory leaks problems that demands investigation. I will shed some light on this issue in the next few posts.

    The Java GC process is a low-priority thread and it constantly searches memory for unreachable objects that are not reachable by any live thread. Different JVMs use different algorithms to determine how to collect ...
    Categories
    Performance
  22. Subclipse - Checkout

    by , 05-25-2011 at 11:02 AM (My Java Tips)
    Subclipse is a popular SVN plugin for Eclipse. This post is about how to do checkout using Subclipse.

    You will check out code from the trunk or branch of a project. First configure your repositories. Open the SVN Repository Perspective and then open the contents of the repository where your project is located. Locate the trunk/ or branch/ directory in the project and right-click the trunk/ directory or branch/ directory, and choose Checkout from the context-menu. You will be see a ...
    Categories
    Subversion
  23. Committing code in Subversion

    by , 05-25-2011 at 11:00 AM (My Java Tips)
    Committing means persisting your changes. You may commit one file or multiple files at once.

    Subclipse makes committing easy and simple. When a file is modified, Subclipse displays an icon next to the file name (asterisk) which indicates that your local copy is different from what was in the repository
    when the file was checked out. This helps you know what to commit. Remember, if you create a new file, it will have a different icon(a small question-mark). The "changed" ...
    Tags: subclipse Add / Edit Tags
    Categories
    Subversion
  24. Using interfaces in APIs

    by , 05-25-2011 at 10:59 AM (My Java Tips)
    When you are designing your API, you might want to use interface since this seems a common practice. There exist a style in Java programming where everything is expressed in terms of Java interfaces instead of classes.

    Using interfaces provides benefits but its not a good idea for an entire API to be expressed in terms of them.

    Use interface as a type if you have a good reason for it to be. Following are some important facts to be considered:

    Interfaces ...
    Tags: api, interface Add / Edit Tags
    Categories
    Java SE
  25. Writing deployment descriptor

    by , 05-25-2011 at 10:57 AM (My Java Tips)
    If you writing J2EE applications, you need to deploy your application to J2EE container like JBOSS. I will shed some light on how to write simple deployment descriptors for your web applications.

    I am deploying my JSP application on JBOSS. For JBOSS, we need to put our application in WAR format in deploy folder. So deployment descriptor will have following 2 tasks:

    1. Archieve the application into WAR
    2. Copy that WAR to deploy folder of JBOSS


    Assuming ...
    Tags: j2ee, war Add / Edit Tags
    Categories
    Java EE , JBoss
  26. Wrapping JDBC Objects

    by , 05-25-2011 at 10:55 AM (My Java Tips)
    In Glassfish connection pool has a property called "Wrap JDBC Objects" which can have value true or false. You may define it in domain.xml file.

    This property is defined as: "When set to true, application will get wrapped jdbc objects for Statement, PreparedStatement, CallableStatement, ResultSet, DatabaseMetaData".

    If you follow JDBC specifications, you will be see that obtaining connection using DataSource.getConnection() or other objects like ...
  27. hibernate.connection.rele ase_mode

    by , 05-25-2011 at 10:49 AM (My Java Tips)
    Hibernate JDBC/Connection's property hibernate.connection.release_mode is used to specify when Hibernate should release JDBC connections.

    A JDBC connection is held until the session is explicitly closed or disconnected by default. For a JTA datasource, one should use after_statement to aggressively release connections after every JDBC call. And for a non-JTA connection, it often makes sense to release the connection at the end of each transaction. after_transaction.auto will
    ...
    Categories
    Hibernate
  28. Hibernate Dialects

    by , 05-25-2011 at 10:46 AM (My Java Tips)
    To set SQL dialect, one has to set the hibernate.dialect property to correct org.hibernate.dialect.Dialect subclass for the database.

    Specify a dialect and then Hibernate will use defaults for some of the properties and this will save the effort of specifying them manually.

    Following dialects are supported.

    • DB2 org.hibernate.dialect.DB2Dialect
    • DB2 AS/400 org.hibernate.dialect.DB2400Dialect
    • DB2 OS390 org.hibernate.dialect.DB2390Dialect
    • PostgreSQL
    ...
  29. JSP Tags

    by , 05-25-2011 at 10:32 AM (My Java Tips)
    JSP tags are like HTML tags and they do not use <%. Instead they use < character.

    JSP tags can have a "start tag", a "tag body" and an "end tag". As you may have guessed, the start and end tag both use the tag name, enclosed in < and > characters. Just has HTNL tags, the ending JSP tag starts with a / character after the < character. The tag names have an embedded colon character : in them, the part before the colon describes the type ...
    Tags: jsp tags Add / Edit Tags
    Categories
    JSP
  30. XSLT Conditions

    by , 05-25-2011 at 10:30 AM (My Java Tips)
    Consider the scenario: You have xml data and you need to filter some data out. You may do that in Java but its open a good idea to keep the logic separate in XSLT filters.

    if-then-else condition is done the following way in XSLT:

    Java Code:
    <xsl:choose>
      <xsl:when test="...">
        ...
      </xsl:when>
      <xsl:when test="...">
        ...
      </xsl:when>
      <xsl:otherwise>
    ...
    Tags: xslt Add / Edit Tags
    Categories
    XML
Page 13 of 15 FirstFirst ... 31112131415 LastLast