Featured FREE Whitepapers

What's New Here?

jcg-logo

Best Of The Week – 2011 – W10

Hello guys, Time for the “Best Of The Week” links for the week that just passed. Here are some links that drew JavaCodeGeeks attention: * Why do I have this long GC pause?: An article where the author discusses how he performed Garbage Collection diagnosis and troubleshooting in order to fix big delays in GC. Some of the inner details of GC are explained and lots of hints and tips are provided. * Implementing Ajax Authentication using jQuery, Spring Security and HTTPS: Title says it all. How to implement autentication in an Ajax based page using jQuery, Spring Security and HTTPS. Some issues that the author encountered are also analyzed in the end. After this, also check our article Securing GWT apps with Spring Security. * How to Dramatically Speed Up Your Web Application: An Introduction to memcached: An introductory article to Memcached, a free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. We have many caching options in Java, but Memcached is quite generic in nature and is totally worth to check out. * A Practical Guide to Varnish – Why Varnish Matters: On the similar topic of scalability, this is a short guide to Varnish, an open source, high performance http accelerator that sits in front of a web stack and caches pages. This caching layer is very configurable and can be used for both static and dynamic content. * Adopting the JVM: An InfoQ presentation where advice is offered to those interested in using the JVM with languages other than Java, talking about language interoperability, polyglot patterns, build tools, tweaking the JVM, and JVM tools. * Behind the Scenes of Serialization in Java: An article on one of the most discussed issues ever, the one of serialization. The author provides an overview of the various existing alternatives. He omits Google’s Proto Buffers solution, but it is a good read nonetheless. Also check our article Java Best Practices – High performance Serialization. * A parallel strategy for ‘millions of developers’: A short article discussing one of the proposed approaches for parallel programming which in theory could alleviate some of the developers burden. Parallel programming constantly gains ground and is about to change the way we write code. Don’t forget to check out our relevant article Java Fork/Join for Parallel Programming. * Real Life Ajax Troubleshooting Guide: I am always fascinated by troubleshooting and debugging stories and this one is very interesting. Read how these guys managed to pinpoint a difficult to discover bug in an Ajax based web application. Also check Debugging a Production Server – Eclipse and JBoss showcase. * Stack Overflow Architecture Update – Now at 95 Million Page Views a Month: High Scalability again (I just love their articles) on an article discussing some of the latest architectural changes in StackOverflow’s stack of technologies. StackOverflow has been using a Microsoft based stack but they are slowly shifting to more open technologies like Linux and NoSQL. * Spring Web Services 2.0: Best Java Stack for SOAP-based Web Services: A short article discussing some of the new features in Spring’s Web Services stack. After that, also check JAX–WS with Spring and Maven Tutorial. That’s all for this week. Stay tuned for more, here at JavaCodeGeeks. Related Articles:Best Of The Week – 2011 – W09 Best Of The Week – 2011 – W08 Best Of The Week – 2011 – W07 Best Of The Week – 2011 – W06 Best Of The Week – 2011 – W05...
spring-security-logo

Java Code Geeks Andygene Web Archetype

Hello all, We are happy to announce that the first release of a set of maven archetypes is out!!! The goal of this set is to provide project templates that can fulfill various development needs. You can find the JCG roadmap at the end if this post. This first release aims to provide a project template – architecture for web applications. The name of the archetype is jcg-andygene-archetype and it is hosted at the following location: http://code.google.com/p/jcg-andygene-web/ The license for this set of archetypes is Apache License. Developers are free to change it. Archetype FeaturesSpring MVC Spring Security Dojo JasperReports Drools Activiti CXF ActiveMQ Ehcache Hibernate JPA2Featured Description Spring MVC: Spring’s Web MVC framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for upload files. Spring Security: Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications Dojo: Javascript library for cool user interfaces. JasperReports: Powerful reporting tool. It can generate reports in a various set of formats. Drools: Powerful business logic platform . Drools use Rete algorithm for decisions and can be dynamically be configured to change business logic of your application. Activiti: Powerful bpmn2 implementation that users can use as a link between business people and developers. CXF: Web Services stack. ActiveMQ: JMS implementation .The Java Message Service (JMS) API is a Message Oriented Midleware (MOM) API for sending messages between two or more clients. Ehcache:Second level cache that can boost your application. Hibernate JPA2: Implementation of JPA2 Specification. An Object Relational Map. Archetype RequirementsApache Maven 2 Tomcat 6 (Optional) A development toolset (e.g. Eclipse or SpringSourceTool Suite or any other toolset. For conveniency it would be desirable to support maven integration) for viewing the created project architecture and for developing based on this archetype.Installation Instructions Download the archetype. Extract it somewhere in your disk and type the following in the root folder of the archetype: mvn installGenerating Your First Project To generate you first project type the following in your workspace directory: mvn archetype:generate -DarchetypeCatalog=local -DarchetypeGroupId=com.javacodegeeks -DarchetypeArtifactId=jcg-andygene-web-archetypeYou will be prompted for several parameters of the archetype. An example is the following: Define value for property 'groupId': : com.javacodegeeks Define value for property 'artifactId': : MyFirstJCGProject Define value for property 'version': 1.0-SNAPSHOT: 1.0.0.Final Define value for property 'package': com.javacodegeeks: com.javacodegeeks [INFO] Using property: activemqpersistencedir = ~/activemq [INFO] Using property: activemqpersistencesize = 32mb [INFO] Using property: activemqsessioncachesize = 100 Define value for property 'activitipassword': : nothing [INFO] Using property: activitiurl = jdbc\:h2\:mem/activiti [INFO] Using property: activitiusername = sa Define value for property 'databasepassword': : nothing [INFO] Using property: databaseurl = jdbc\:h2\:mem/andygene [INFO] Using property: databaseusername = sa [INFO] Using property: db = h2 [INFO] Using property: tomcathost = localhost:8080 Confirm properties configuration: groupId: com.javacodegeeks artifactId: MyFirstJCGProject version: 1.0.0.Final package: com.javacodegeeks activemqpersistencedir: ~/activemq activemqpersistencesize: 32mb activemqsessioncachesize: 100 activitipassword: nothing activitiurl: jdbc\:h2\:mem/activiti activitiusername: sa databasepassword: nothing databaseurl: jdbc\:h2\:mem/andygene databaseusername: sa db: h2 tomcathost: localhost:8080This will generate a maven project with name MyFirstJCGProject (Depending on the parameter you provided before) Below the structure of the main elements of this project is presented: Under src/main/java:dao: It contains the Data Access Objects (DAOs) domain: It contains the Data Transfer Objects (DTOs) drools: It contains the drool object used. jms: It contains the JMS listener and sender. service and service/impl: It contains the main services. util: It contains various utilities (e.g. validation) web: It contains the controllers. webservices: It contains the soap interfaces.Under src/main/resources:activiti: It contains the flows. drools: It contains the rules. META-INF: It contains the persistence.xml. META-INF/spring: It contains properties files and application context for spring relates sources.Under src/test:java: It contains the unit tests. Resources: It contains log4j.Under src/main/webapp (files for the we application):/WEB-INF/spring: It contains the spring descriptor for web application. /WEB-INF/layouts: It contains the web templates. /WEB-INF/reports: It contains the jasper reports. /WEB-INF/classes: It contains the web properties. /WEB-INF/views: It contains the structure of the web pages.Running/building Your First Project To run – build your project, type the following (if your development toolkit has maven integration you can perform the following action from inside your toolkit): cd MyFirstJCGProject mvn installA war file will be generated at target/MyFirstJCGProject.war . Copy this file at TOMCAT_HOME/webapps. Start Tomcat and point the browser at the following location http://tomcathost/MyFirstJCGProject Tomcat Configuration You must edit catalina.sh, for mac or linux ,or catalina.bat, if you are running on windows, located at TOMCAT_HOME/bin and add the following lines: JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m -Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DisableExplicitGC"Notice: Drools require connection to the internet .If you want to run your project without having access to the internet copy MyFirstJCGProject/src/main/resources/META-INF/spring/drools-spring-1.0.0.xsd to a location to your drive and edit MyFirstJCGProject/src/main/resources/META-INF/spring/applicationContext-drools.xml : Change http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd to file:///path/to/drools-spring-1.0.0.xsd. Archetype Supported Databases:H2(default) db property value [h2] Postgres db property value [postgres] Mysql db property value [mysql] Oracle db property value [oracle]Notice: Case you choose oracle you must download oracle, oracle driver and install it to your repository. JCG Roadmap: Java Code Geeks plans to provide more tools to the community in the near future. The following projects are in the Roadmap: jcg-andygene-soa-archetype:Spring Integration Spring Batch Drools Activiti Smooksjcg-andygene-nosql-archetype:MongoDB HBase(hadoop) jcg-andygene-jcr-archetype:JackRabbitjcg-andygene-telecom-archetype:Mobicents Sip Servlets SMS/MMSjcg-andygene-console:Human Task Manager JMX Console Rule Hot Deployment Smooks Hot Deployment Activiti Hot Deploymentappboost:Eclipse plugin that combines the above technologies for rapid development of applications.Related Articles:JAX–WS with Spring and Maven Tutorial Buiding your own GWT Sping Maven Archetype   Spring MVC Development – Quick Tutorial Spring 3 RESTful Web Services  Securing GWT apps with Spring Security...
java-logo

Design Patterns in the JDK

Brian Du Preez, our JCG partner from Zen in the art of IT has made a very fine job of collecting the most common design patterns found in the JDK. The pattern list is indeed impressive and long, so let us babble no more and present it to you. I saw an article (well more of a rant) the other day, by Rob Williams Brain Drain in enterprise Dev. I have to say, I do agree with some of what he is saying. I know from my personal experience, I had spent a good 2 or so years just wallowing in the enterprise development world, not learning anything and actually losing my skills I developed before. The corporate confront zone is not conducive to eager technologists. In this article he also stated: “1 in 10 cant even pass a simple test like ‘which design pattern is used in the streams library that makes BufferedReader interchangeable with a FileReader?’” I also tested it at work and I only had 1 out of the 8 people asked that got it right Without much confidence, I had guessed Decorator based on “interchangeable”. I then decided that was actually some worth sneaking into future interviews, and probably a good time to revise a little. So I went scouring the internet to find all I could on the topic and I didn’t actually find as much as I thought I would. Most of it came from BalusC at Stackoverflow, the rest was very scattered between blog posts, java ranch, some old pdf’s and articles I had. I didn’t take every single example of every single pattern I found, but rather the common ones. This may be a good way for people to learn about patterns, quite often they are using them everyday without realizing. Structural Adapter: This is used to convert the programming interface/class into that of another.java.util.Arrays#asList() javax.swing.JTable(TableModel) java.io.InputStreamReader(InputStream) java.io.OutputStreamWriter(OutputStream) javax.xml.bind.annotation.adapters.XmlAdapter#marshal() javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal()Bridge: This decouples an abstraction from the implementation of its abstract operations, so that the abstraction and its implementation can vary independently.AWT (It provides an abstraction layer which maps onto the native OS the windowing support.) JDBCComposite: Lets clients treat individual objects and compositions of objects uniformly. So in other words methods on a type accepting the same type.javax.swing.JComponent#add(Component) java.awt.Container#add(Component) java.util.Map#putAll(Map) java.util.List#addAll(Collection) java.util.Set#addAll(Collection)Decorator: Attach additional responsibilities to an object dynamically and therefore it is also an alternative to subclassing. Can be seen when creating a type passes in the same type. This is actually used all over the JDK, the more you look the more you find, so the list below is definitely not complete.java.io.BufferedInputStream(InputStream) java.io.DataInputStream(InputStream) java.io.BufferedOutputStream(OutputStream) java.util.zip.ZipOutputStream(OutputStream) java.util.Collections#checked[List|Map|Set|SortedSet|SortedMap]()Facade: To provide a simplified interface to a group of components, interfaces, abstractions or subsystems.java.lang.Class javax.faces.webapp.FacesServletFlyweight: Caching to support large numbers of smaller objects efficiently. I stumbled apon this a couple months back.java.lang.Integer#valueOf(int) java.lang.Boolean#valueOf(boolean) java.lang.Byte#valueOf(byte) java.lang.Character#valueOf(char)Proxy: The Proxy pattern is used to represent with a simpler object an object that is complex or time consuming to create.java.lang.reflect.Proxy RMICreational Abstract factory: To provide a contract for creating families of related or dependent objects without having to specify their concrete classes. It enables one to decouple an application from the concrete implementation of an entire framework one is using. This is also found all over the JDK and a lot of frameworks like Spring. They are simple to spot, any method that is used to create an object but still returns a interface or abstract class.java.util.Calendar#getInstance() java.util.Arrays#asList() java.util.ResourceBundle#getBundle() java.sql.DriverManager#getConnection() java.sql.Connection#createStatement() java.sql.Statement#executeQuery() java.text.NumberFormat#getInstance() javax.xml.transform.TransformerFactory#newInstance()Builder: Used simplify complex object creation by defining a class whose purpose is to build instances of another class. The builder pattern also allows for the implementation of a Fluent Interface.java.lang.StringBuilder#append() java.lang.StringBuffer#append() java.sql.PreparedStatement javax.swing.GroupLayout.Group#addComponent()Factory method: Simply a method that returns an actual type.java.lang.Proxy#newProxyInstance() java.lang.Object#toString() java.lang.Class#newInstance() java.lang.reflect.Array#newInstance() java.lang.reflect.Constructor#newInstance() java.lang.Boolean#valueOf(String) java.lang.Class#forName()Prototype: Allows for classes whose instances can create duplicates of themselves. This can be used when creating an instance of a class is very time-consuming or complex in some way, rather than creating new instances, you can make copies of the original instance and modify it.java.lang.Object#clone() java.lang.CloneableSingleton: This tries to ensure that there is only a single instance of a class. I didn’t find an example but another solution would be to use an Enum as Joshua Bloch suggests in Effective Java.java.lang.Runtime#getRuntime() java.awt.Toolkit#getDefaultToolkit() java.awt.GraphicsEnvironment#getLocalGraphicsEnvironment() java.awt.Desktop#getDesktop()Behavioral Chain of responsibility: Allows for the decoupling between objects by passing a request from one object to the next in a chain until the request is recognized. The objects in the chain are different implementations of the same interface or abstract class.java.util.logging.Logger#log() javax.servlet.Filter#doFilter()Command: To wrap a command in an object so that it can be stored, passed into methods, and returned like any other object.java.lang.Runnable javax.swing.ActionInterpreter: This pattern generally describes defining a grammar for that language and using that grammar to interpret statements in that format.java.util.Pattern java.text.Normalizer java.text.FormatIterator: To provide a consistent way to sequentially access items in a collection that is independent of and separate from the underlying collection.java.util.Iterator java.util.EnumerationMediator: Used to reduce the number of direct dependencies between classes by introducing a single object that manages message distribution.java.util.Timer java.util.concurrent.Executor#execute() java.util.concurrent.ExecutorService#submit() java.lang.reflect.Method#invoke()Memento: This is a snapshot of an object’s state, so that the object can return to its original state without having to reveal it’s content. Date does this by actually having a long value internally.java.util.Date java.io.SerializableNull Object: This can be used encapsulate the absence of an object by providing an alternative ‘do nothing’ behavior. It allows you to abstract the handling of null objects.java.util.Collections#emptyList() java.util.Collections#emptyMap() java.util.Collections#emptySet()Observer: Used to provide a way for a component to flexibly broadcast messages to interested receivers.java.util.EventListener javax.servlet.http.HttpSessionBindingListener javax.servlet.http.HttpSessionAttributeListener javax.faces.event.PhaseListenerState: This allows you easily change an object’s behavior at runtime based on internal state.java.util.Iterator javax.faces.lifecycle.LifeCycle#execute()Strategy: Is intended to provide a means to define a family of algorithms, encapsulate each one as an object. These can then be flexibly passed in to change the functionality.java.util.Comparator#compare() javax.servlet.http.HttpServlet javax.servlet.Filter#doFilter()Template method: Allows subclasses to override parts of the method without rewriting it, also allows you to control which operations subclasses are required to override.java.util.Collections#sort() java.io.InputStream#skip() java.io.InputStream#read() java.util.AbstractList#indexOf()Visitor: To provide a maintainable, easy way to perform actions for a family of classes. Visitor centralizes the behaviors and allows them to be modified or extended without changing the classes they operate on.javax.lang.model.element.Element and javax.lang.model.element.ElementVisitor javax.lang.model.type.TypeMirror and javax.lang.model.type.TypeVisitorBetter is the enemy of good! Byron Related Articles:Things Every Programmer Should Know 10 Tips for Proper Application Logging Laws of Software Design Java Best Practices Series 9 Tips on Surviving the Wild West Development Process...
jcg-logo

OpenXava Project Lead Joins JavaCodeGeeks

Hi guys, I am pleased to announce that Javier Paniza, project lead for the OpenXava project, has joined our JCG program. Javier has been developing with Java Enterprise since 1998 and occasionally blogs at Sys-Con. Let me tell you a few things about OpenXava too. As described in the official site: OpenXava is a tool for Rapid Java Web Development, well-suited for business and database oriented applications. Here are some key points regarding the project:Open Source (LGPL) More than 130,000 downloads 40 contributors Active community High productivity Short learning curve AJAX User Interface Exhaustive documentation Based in Java standardsThe project is very mature and currently is at major version 4. You can take a look at the demos or get your hands dirty with the quick start guide. We are certainly waiting for some high-quality material from Javier! In case you have used the OpenXava framework in the past, I would love to hear your experiences. Cheers, Ilias ...
apache-derby-logo

DataNucleus 3.0 vs Hibernate 3.5

DataNucleus Access Platform, as stated at the official product site, is the most standards-compliant Open Source Java persistence product in existence. It is fully compliant with the JDO1, JDO2, JDO2.1, JDO2.2, JDO3, JPA1 and JPA2 Java standards. It also complies with the OGC Simple Feature Specification for persistence of geospatial Java types to RDBMS. It utilities an OSGi-based plugin mechanism meaning that it is extremely extensible. Hibernate, as stated at the official “about” page of the product, is a high-performance Object/Relational persistence and query service. The most flexible and powerful Object/Relational solution on the market, Hibernate takes care of the mapping from Java classes to database tables and from Java data types to SQL data types. It provides data query and retrieval facilities that significantly reduce development time. For the purpose of this article we will use the aforementioned, well known, products as the actual implementations of the persistence API. Our goal is to be able to compare their performance when applying CRUD (Create – Retrieve – Update – Delete) operations against a database. To do so, we are going to implement two distinct Spring based WEB applications that will act as our “test bases”. The two applications will be identical in terms of their service and data access layer definitions – interfaces and implementation classes. For data access we will utilize JPA2 as the Java Persistence API. For the database we will use an embedded Derby instance. Last but not least we are going to implement and perform the same suit of performance tests against the five “basic” data access operations described below :Persist a record Retrieve a record by its ID Retrieve all records Update an existing record Delete a recordAll tests are performed against a Sony Vaio with the following characteristics :System : openSUSE 11.1 (x86_64) Processor (CPU) : Intel(R) Core(TM)2 Duo CPU T6670 @ 2.20GHz Processor Speed : 1,200.00 MHz Total memory (RAM) : 2.8 GB Java : OpenJDK 1.6.0_0 64-BitThe following tools are used :Spring framework 3.0.1 Apache Derby 10.6.1.0 Hibernate 3.5.1 DataNucleus 3.0.0-m1 c3p0 0.9.1.2 Java Benchmarking framework from Brent BoyerFinal notices :You can download the full source code for the two “test bases” here and here. These are Eclipse – Maven based projects. In order to be able to compile and run the tests yourself you will need to install the Java Benchmarking framework binary – jar files to your Maven repository. Alternatively, as a “one click” solution, you may use the Java Benchmarking Maven bundle created by us. You can download it from here, unzip it to your Maven repository and you are good to go.The “test bases” … We will start by providing information on how we have implemented the “test base” projects. This is imperative in order to be crystal clear about the specifics of the environment our tests will run against. As previously stated, we have implemented two multi-tier Spring based WEB applications. In each application two layers have been implemented, the Service Layer and the Data Access Layer. These layers have identical definitions – interfaces and implementation specifics. Our domain model consists of just an “Employee” object. The Service Layer provides a trivial “business” service that exposes CRUD (Create – Retrieve – Update – Delete) functionality for the “Employee” object whereas the Data Access Layer consists of a trivial Data Access Object that utilizes Spring JpaDaoSupport abstraction in order to provide the actual interoperability with the database. Below are the Data Access Layer specific classes : import javax.annotation.PostConstruct; import javax.persistence.EntityManagerFactory;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository;import com.javacodegeeks.springdatanucleus.dto.EmployeeDTO;@Repository("employeeDAO") public class EmployeeDAO extends JpaDAO<Long, EmployeeDTO> { @Autowired EntityManagerFactory entityManagerFactory; @PostConstruct public void init() { super.setEntityManagerFactory(entityManagerFactory); } }As you can see our Data Access Object (DAO) extends the JpaDAO class. This class is presented below : import java.lang.reflect.ParameterizedType; import java.util.List;import javax.persistence.EntityManager; import javax.persistence.PersistenceException; import javax.persistence.Query;import org.springframework.orm.jpa.JpaCallback; import org.springframework.orm.jpa.support.JpaDaoSupport;public abstract class JpaDAO<K, E> extends JpaDaoSupport { protected Class<E> entityClass;@SuppressWarnings("unchecked") public JpaDAO() { ParameterizedType genericSuperclass = (ParameterizedType) getClass() .getGenericSuperclass(); this.entityClass = (Class<E>) genericSuperclass .getActualTypeArguments()[1]; }public void persist(E entity) { getJpaTemplate().persist(entity); }public void remove(E entity) { getJpaTemplate().remove(entity); } public E merge(E entity) { return getJpaTemplate().merge(entity); } public void refresh(E entity) { getJpaTemplate().refresh(entity); }public E findById(K id) { return getJpaTemplate().find(entityClass, id); } public E flush(E entity) { getJpaTemplate().flush(); return entity; } @SuppressWarnings("unchecked") public List<E> findAll() { Object res = getJpaTemplate().execute(new JpaCallback() {public Object doInJpa(EntityManager em) throws PersistenceException { Query q = em.createQuery("SELECT h FROM " + entityClass.getName() + " h"); return q.getResultList(); } }); return (List<E>) res; }@SuppressWarnings("unchecked") public Integer removeAll() { return (Integer) getJpaTemplate().execute(new JpaCallback() {public Object doInJpa(EntityManager em) throws PersistenceException { Query q = em.createQuery("DELETE FROM " + entityClass.getName() + " h"); return q.executeUpdate(); } }); } }Following is our domain class, the EmployeeDTO class : import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table;@Entity @Table(name = "EMPLOYEE") public class EmployeeDTO implements java.io.Serializable { private static final long serialVersionUID = 7440297955003302414L;@Id @Column(name="employee_id") private long employeeId; @Column(name="employee_name", nullable = false, length=30) private String employeeName; @Column(name="employee_surname", nullable = false, length=30) private String employeeSurname; @Column(name="job", length=50) private String job; public EmployeeDTO() { }public EmployeeDTO(int employeeId) { this.employeeId = employeeId; }public EmployeeDTO(long employeeId, String employeeName, String employeeSurname, String job) { this.employeeId = employeeId; this.employeeName = employeeName; this.employeeSurname = employeeSurname; this.job = job; }public long getEmployeeId() { return employeeId; }public void setEmployeeId(long employeeId) { this.employeeId = employeeId; }public String getEmployeeName() { return employeeName; }public void setEmployeeName(String employeeName) { this.employeeName = employeeName; }public String getEmployeeSurname() { return employeeSurname; }public void setEmployeeSurname(String employeeSurname) { this.employeeSurname = employeeSurname; }public String getJob() { return job; }public void setJob(String job) { this.job = job; } }Last but not least the “business” service interface and implementation classes are presented below : import java.util.List;import com.javacodegeeks.springdatanucleus.dto.EmployeeDTO;public interface EmployeeService { public EmployeeDTO findEmployee(long employeeId); public List<EmployeeDTO> findAllEmployees(); public void saveEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception; public void updateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception; public void saveOrUpdateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception; public void deleteEmployee(long employeeId) throws Exception; }import java.util.List;import javax.annotation.PostConstruct; import javax.annotation.PreDestroy;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional;import com.javacodegeeks.springdatanucleus.dao.EmployeeDAO; import com.javacodegeeks.springdatanucleus.dto.EmployeeDTO; import com.javacodegeeks.springdatanucleus.services.EmployeeService;@Service("employeeService") public class EmployeeServiceImpl implements EmployeeService { @Autowired private EmployeeDAO employeeDAO;@PostConstruct public void init() throws Exception { } @PreDestroy public void destroy() { }public EmployeeDTO findEmployee(long employeeId) { return employeeDAO.findById(employeeId); } public List<EmployeeDTO> findAllEmployees() { return employeeDAO.findAll(); } @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) public void saveEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception { EmployeeDTO employeeDTO = employeeDAO.findById(employeeId); if(employeeDTO == null) { employeeDTO = new EmployeeDTO(employeeId, name,surname, jobDescription); employeeDAO.persist(employeeDTO); } } @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) public void updateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception { EmployeeDTO employeeDTO = employeeDAO.findById(employeeId); if(employeeDTO != null) { employeeDTO.setEmployeeName(name); employeeDTO.setEmployeeSurname(surname); employeeDTO.setJob(jobDescription); }} @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) public void deleteEmployee(long employeeId) throws Exception { EmployeeDTO employeeDTO = employeeDAO.findById(employeeId); if(employeeDTO != null) employeeDAO.remove(employeeDTO);} @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) public void saveOrUpdateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception {EmployeeDTO employeeDTO = new EmployeeDTO(employeeId, name,surname, jobDescription); employeeDAO.merge(employeeDTO); }}What follows is the “applicationContext.xml” file that drives the Spring IoC container. The contents of this file is also identical between the two “test base” projects. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"><context:component-scan base-package="com.javacodegeeks.springdatanucleus" /><tx:annotation-driven /><bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="MyPersistenceUnit" /> </bean><bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean></beans>To be able to launch the Spring application from a Servlet container (do not forget that we have implemented Spring based WEB applications) we have included the following listener into the “web.xml” file for both our “test base” applications : <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>The only file that is different between the two “test base” projects is the one that defines the actual implementation of the Java Persistent API (JPA) to be used – the “persistence.xml” file. Below is the one that we have used to utilize DataNucleus Access Platform : <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider> <class>com.javacodegeeks.springdatanucleus.dto.EmployeeDTO</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="datanucleus.storeManagerType" value="rdbms"/> <property name="datanucleus.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver"/> <property name="datanucleus.ConnectionURL" value="jdbc:derby:runtime;create=true"/> <!-- <property name="datanucleus.ConnectionUserName" value=""/> <property name="datanucleus.ConnectionPassword" value=""/> --> <property name="datanucleus.autoCreateSchema" value="true"/> <property name="datanucleus.validateTables" value="false"/> <property name="datanucleus.validateConstraints" value="false"/><property name="datanucleus.connectionPoolingType" value="C3P0"/> <property name="datanucleus.connectionPool.minPoolSize" value="5" /> <property name="datanucleus.connectionPool.initialPoolSize" value="5" /> <property name="datanucleus.connectionPool.maxPoolSize" value="20" /> <property name="datanucleus.connectionPool.maxStatements" value="50" /> </properties> </persistence-unit> </persistence>What follows is the “persistence.xml” file that we have used to utilize Hibernate as our JPA2 implementation framework : <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.hibernatePersistence</provider> <class>com.javacodegeeks.springhibernate.dto.EmployeeDTO</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.show_sql" value="false" /> <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" /> <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver" /> <property name="hibernate.connection.url" value="jdbc:derby:runtime;create=true" /> <!-- <property name="hibernate.connection.username" value="" /> <property name="hibernate.connection.password" value="" /> --><property name="hibernate.c3p0.min_size" value="5" /> <property name="hibernate.c3p0.max_size" value="20" /> <property name="hibernate.c3p0.timeout" value="300" /> <property name="hibernate.c3p0.max_statements" value="50" /> <property name="hibernate.c3p0.idle_test_period" value="3000" /> </properties> </persistence-unit> </persistence>Finally we demonstrate the class that implements all test cases to be executed. This class is identical for both the “test base” projects : import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail;import java.util.List; import java.util.concurrent.Callable;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import bb.util.Benchmark;import com.javacodegeeks.springhibernate.dto.EmployeeDTO; import com.javacodegeeks.springhibernate.services.EmployeeService;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/applicationContext.xml"}) public class EmployeeServiceTest {@Autowired EmployeeService employeeService; @Test public void testSaveEmployee() { try { employeeService.saveEmployee(1, "byron", "kiourtzoglou", "master software engineer"); employeeService.saveEmployee(2, "ilias", "tsagklis", "senior software engineer"); } catch (Exception e) { fail(e.getMessage()); } } @Test public void testFindEmployee() { assertNotNull(employeeService.findEmployee(1)); } @Test public void testFindAllEmployees() { assertEquals(employeeService.findAllEmployees().size(), 2); }@Test public void testUpdateEmployee() { try { employeeService.updateEmployee(1, "panagiotis", "paterakis", "senior software engineer"); assertEquals(employeeService.findEmployee(1).getEmployeeName(), "panagiotis"); } catch (Exception e) { fail(e.getMessage()); } }@Test public void testDeleteEmployee() { try { employeeService.deleteEmployee(1); assertNull(employeeService.findEmployee(1)); } catch (Exception e) { fail(e.getMessage()); } }@Test public void testSaveOrUpdateEmployee() { try { employeeService.saveOrUpdateEmployee(1, "byron", "kiourtzoglou", "master software engineer"); assertEquals(employeeService.findEmployee(1).getEmployeeName(), "byron"); } catch (Exception e) { fail(e.getMessage()); } } @Test public void stressTestSaveEmployee() { Callable<Integer> task = new Callable<Integer>() { public Integer call() throws Exception { int i; for(i = 3;i < 2048; i++) { employeeService.saveEmployee(i, "name-" + i, "surname-" + i, "developer-" + i); } return i; } }; try { System.out.println("saveEmployee(...): " + new Benchmark(task, false, 2045)); } catch (Exception e) { fail(e.getMessage()); }assertNotNull(employeeService.findEmployee(1024)); } @Test public void stressTestFindEmployee() { Callable<Integer> task = new Callable<Integer>() { public Integer call() { int i; for(i = 1;i < 2048; i++) { employeeService.findEmployee(i); } return i; } }; try { System.out.println("findEmployee(...): " + new Benchmark(task, 2047)); } catch (Exception e) { fail(e.getMessage()); }} @Test public void stressTestFindAllEmployees() { Callable<List<EmployeeDTO>> task = new Callable<List<EmployeeDTO>>() { public List<EmployeeDTO> call() { return employeeService.findAllEmployees(); } }; try { System.out.println("findAllEmployees(): " + new Benchmark(task)); } catch (Exception e) { fail(e.getMessage()); }} @Test public void stressTestUpdateEmployee() { Callable<Integer> task = new Callable<Integer>() { public Integer call() throws Exception { int i; for(i=1;i<2048;i++) { employeeService.updateEmployee(i, "new_name-" + i, "new_surname-" + i, "new_developer-" + i); } return i; } }; try { System.out.println("updateEmployee(...): " + new Benchmark(task, false, 2047)); } catch (Exception e) { fail(e.getMessage()); } assertEquals("new_name-1", employeeService.findEmployee(1).getEmployeeName());} @Test public void stressTestDeleteEmployee() { Callable<Integer> task = new Callable<Integer>() { public Integer call() throws Exception { int i; for(i = 1;i < 2048; i++) { employeeService.deleteEmployee(i); } return i; } }; try { System.out.println("deleteEmployee(...): " + new Benchmark(task, false, 2047)); } catch (Exception e) { fail(e.getMessage()); } assertEquals(true, employeeService.findAllEmployees().isEmpty());}}The results … All test results are presented in the graph below. The vertical axis represents the mean execution time for each test in microseconds (us) thus lower values are better. The horizontal axis represents the test types. As you can see from the test cases presented above, we insert a total number of 2047 “employee” records to the database. For the retrieval test cases (findEmployee(…) and findAllEmployees(…)) the benchmarking framework performed 60 repeats of each test case in order to calculate statistics. All other test cases are executed just once.As you can see, Hibernate outperforms DataNucleus in every test case. Especially in the retrieval by ID (Find) scenario Hibernate is almost 9 times faster than DataNucleus! To my opinion DataNucleus is a fine platform. It can be used when you want to handle data in all of its forms, wherever it is stored. This goes from persistence of data into heterogeneous data-stores, to providing methods of retrieval using a range of query languages. The main advantage of using such a versatile platform to manage your application data, is that you don’t need to take significant time in learning the oddities of particular data-stores, or query languages. Additionally you can use a single common interface for all of your data, thus your team can concentrate their application development time on adding business logic and let DataNucleus take care of data management issues. On the other hand versatility comes to a cost. Being a “hard-core” Object to Relational Mapping (ORM) framework, Hibernate easily outperformed DataNucleus in all of our ORM tests. Like most of the time, its up to the application architect to decide what best suits his needs – versatility or performance, until the DataNucleus team evolve their product to the point where it can excel Hibernate that is ;-) Happy coding and do not forget to share! Byron Related Articles:Things Every Programmer Should Know 10 Tips for Proper Application Logging Laws of Software Design Java Best Practices Series 9 Tips on Surviving the Wild West Development Process How to Do 100K TPS at Less than 1ms Latency Revving Up Your Hibernate Engine...
jcg-logo

Best Of The Week – 2011 – W09

Hello guys, Time for the “Best Of The Week” links for the week that just passed. Here are some links that drew JavaCodeGeeks attention: * The Holy Grail of Zero Defect Systems: A short article discussing the “ultimate goal” of eliminating all defects from your system and why some times it is better to not fix a bug (short answer: in order to avoid the risk of introducing more bugs). * Spring Best Practices: A nice collection of tips (I would not really characterize them as “best practices”) for our beloved framework, Spring. It includes pretty much everything, from configuration to AOP. While you are at it, also take a look at our Spring related articles. * Is The Second Dotcom Bubble Underway? Ten Telltale Signs: Title says it all. With Facebook and Twitter evaluated at ridiculously high prices, are we running towards a new Dotcom bubble? Not a technical article per-se, but I am sure you will find it at very interesting. * This stuff isn’t taught, you learn it bit by bit as you solve each problem: This is actually a re-post of an answer given in a HackerNews thread regarding building high-scalability systems and how someone can learn how to do it. The answer: you learn it a piece at a time when the various problems hit you in the face. * Prevalence of Non-Java JVM Languages on JVM: A short, yet interesting article, discussing the results of a Java.net poll on how much of your JVM-based programming is in languages other than Java. * Using GWT to Build a Large Web Application: This is a post-mortem explaining how a large web application was built using GWT. Cool read, stating some of the benefits of this approach. * Brownfield Software – Industrial Waste or Business Fertilizer?: InfoQ again, with a very nice presentation that addresses 10 myths related to working on legacy software, attempting to prove that one can make good use of legacy code without having to rewrite the entire thing.We all prefer to work in greenfield projects, but there is apparently an other side too. * Getting GWT to work with Servlet 3 Async Requests: The guys at JTeam explain how to make GWT to play along with a new Servlet v.3 feature, the one that supports async requests. * The pain of enterprise Java development: A very nice post discussing how Java enterprise development can be painful and why some times, we developers also make things worse by inflicting some of the pain on our selves. * Brian Chess on Static Code Analysis: An interview with Brian Chess on the merits of static code analysis and how it helps in building secure software. Static analysis is essentially about finding problems in code without executing it. After this, also take a look at one of our previous articles Using FindBugs to produce substantially less buggy code. That’s all for this week. Stay tuned for more, here at JavaCodeGeeks. Related Articles:Best Of The Week – 2011 – W08 Best Of The Week – 2011 – W07 Best Of The Week – 2011 – W06 Best Of The Week – 2011 – W05...
software-development-2-logo

Performance Anxiety – on Performance Unpredictability, Its Measurement and Benchmarking

Joshua Bloch had a great talk called Performance Anxiety (30min, via Parleys slides also available ) at Devoxx 2010, the main message as I read it wasNowadays, performance is completely non-predictable. You have to measure it and employ proper statistics to get some meaningful results. Microbenchmarking is very, very hard to do correctly. No, you misunderstand me, I mean even harder than that! From the resources: Profilers and result evaluation methods may be very misleading unless used correctly.There has been another blog about it but I’d like to record here more detailed remarks. Today we can’t estimate performance, we must measure it because the systems (JVM, OS, processor, …) are very complex with many different heuristics on various levels and thus the performance is highly unpredictable. This doesn’t apply only to Java, but also to C, C++, even to assembly code. Example: Results during a single JVM run may be consistent (warm-up, then faster) but can vary between JVM executions even by 20%. One of the causes may be Compilation Planning (what’s inlined, …) – it’s done in a background thread and thus is inherently non-deterministic. Therefore don’t estimate but measure and not only that – also do statistical processing of the data (how often diff. values appear, what they are, … – mean, median, standard deviation etc.). “Profilers don’t help much; in fact, they can mislead” – Mytkowicz, Diwan etc. – “Evaluating the Accuracy of Java Pro?lers”, PLDI ’10 – in their experiment, each of 4 leading profilers identified a different hotspot. I’d really recommend you reading the related StackOverflow discussion “If profiler is not the answer, what other choices do we have?” (the answer is: profilers have their value, but use the correct ones and use them correctly). The conclusion of the original paper: Our results are disturbing because they indicate that pro?ler incorrectness is pervasive—occurring in most of our seven benchmarks and in two production JVM—-and signi?cant—all four of the state-of-the-art pro?lers produce incorrect pro?les. Incorrect pro?les can easily cause a performance analyst to spend time optimizing cold methods that will have minimal effect on performance. We show that a proof-of-concept pro?ler that does not use yield points for sampling does not suffer from the above problems.“Benchmarking is really, really hard!” and “Most benchmarks are seriously broken“. Broken means that either the measurement’s error is higher than the value being measured or that the results obtained are unrelated to intended measurements. It seems that it is actually really hard to find a (micro)-benchmark, which isn’t broken. Joshua recommends Cliff Click’s JavaOne 2009 presentation The Art of (Java) Benchmarking (see also an interesting related interview with Cliff), which I belive to have seen and which points out the various traps here. Joshua also mentions that some frameworks, such as Google Caliper may help you to avoid the pitfalls, though I’m quite sure they can’t protect you from all. Joshua mentions a couple of interesting papers, you should check the slides for them. One which sounds really interesting to me is by Georges, Buytaert and Eeckhout – Statistically Rigorous Java Performance Evaluation, OOPSLA07 (20 pages). They mention there that you need to run VM 30 times to get meaningful data. From the abstract: This paper shows that prevalent methodologies can be misleading, and can even lead to incorrect conclusions. The reason is that the data analysis is not statistically rigorous. In this paper, we present a survey of existing Java performance evaluation methodologies and discuss the importance of statistically rigorous data analysis for dealing with non-determinism. We advocate approaches to quantify startup as well as steady-state performance, and, in addition, we provide the JavaStats software to automatically obtain performance numbers in a rigorous manner. Although this paper focuses on Java performance evaluation, many of the issues addressed in this paper also apply to other programming languages and systems that build on a managed runtime system.Personal touch I find this subject very interesting because for over a year I’m involved in performance optimization of one of our data feeds, which used to run for couple of days (latest results: 1/2h [with a bit of cheating]). My experience completely supports what Joshua says – don’t guess but measure, profilers may be misleading, performance is unpredictable. Though as a collegue mentioned, in the domain of enterprise Java, our performance problems are usually caused by the database and communication with it (which 100% applies to that feed too). I’ve already blogged about some experiences, e.g. in The power of batching or speeding JDBC by 100 (inspired by JDBC performance tuning with fetch size), check also the performance tag for interesting links. I also appreciated and applied the knowledge from Accurately computing running variance (I often wish I have slept less and paid attention more during the uni math lectures ). Conclusion The higher complexity, the higher unpredictabilityAs an application programmer, use high-level, declarative constructs where posible to push the responsability for performance one level down to library and JVM authors, who should know better. Measure repeatedly and process the results with proper statistics. Don’t forget to repeat them over time, the platform evolves with every release.Once again, microbenchmarking is hard! If you have to play with it, use something like Caliper and be aware that your results are most likely wrong anyway. Disclaimer Personally I would like to thank our JCG partner Jakub, for sharing quality information with the community, since this article was originally posted on his web log “The Holy Java“. Furthermore I strongly suggest you should test out the open source Java Benchmarking framework from Brent Boyer which you can download from his site here. You should also read the “Robust Java benchmarking” series of articles (Part 1: Issues and Part 2: Statistics and solutions) Brent has published at IBM developerWorks. Code is here … code is there … code is everywhere ;-) Do not forget to share! Byron Related Articles:Things Every Programmer Should Know 10 Tips for Proper Application Logging Laws of Software Design Java Best Practices Series 9 Tips on Surviving the Wild West Development Process How to Do 100K TPS at Less than 1ms Latency Revving Up Your Hibernate Engine Cajo, the easiest way to accomplish distributed computing in Java...
findbugs-logo

Using FindBugs to produce substantially less buggy code

Some time ago a user of Java-monitor, the forum of our JCG partner, Kees Jan, spotted that his system was forcing a large number of full Garbage Collections despite the fact that the overall memory utilization was low. A rough estimation for the cause of the problem suggested a potential call to System.gc() performed by one of the libraries in use. Lets see what our partner has to say about clarifying similar issues in order to produce substantially less buggy code. … It did occur to me that there is a tool that could have helped with the System.gc() issue. It is aptly named FindBugs. Actually, there are several (PMD and CheckStyle being other, similar tools), but FindBugs is nice because you don’t actually need the application’s source code to check it for bugs. All you need are the JAR files and/or class files. Another great feature is that each report is documented very well. You don’t just get vague warnings that you sort-of give up on after a few minutes, but you can find an explanation for each warning on-line. Here is a sample output of FindBugs, when you would run it against Java-monitor’s probe. $ findbugs -textui -effort:max java-monitor-probes/build/WEB-INF/classes The following classes needed for analysis were missing: javax.servlet.http.HttpServlet javax.servlet.Filter javax.servlet.http.HttpServletResponse javax.servlet.ServletException javax.servlet.ServletConfig javax.servlet.FilterConfig javax.servlet.ServletRequest javax.servlet.ServletResponse javax.servlet.FilterChain Missing classes: 7From this report you can see that FindBugs cannot find any issues with the code itself. There are some classes that the code depends on. These were not included in the analysis. Since these are all Sun-provided classes I presume them to be bugfree. *cough* Anyway. Let’s make the analysis a bit more juicy. Here is the output when running FindBugs against the well-known MySQL JDBC driver. I’m sure that many of you use it in production today. This code produces significantly more results and FindBugs takes a while to analyse the whole package. I have shown only a few lines of the 154 warnings. $ findbugs -textui -effort:max mysql-connector-java-5.1.5-bin.jar ...... H C NP: Method call in com.mysql.jdbc.profiler.ProfilerEvent.pack() passes null for unconditionally dereferenced parameter of writeBytes(byte[], byte[], int) Method invoked at ProfilerEvent.java:[line 375] ...... M D NP: Possible null pointer dereference of s1 on path that might be infeasible in com.mysql.jdbc.ConnectionImpl.nullSafeCompare(String, String) Dereferenced at ConnectionImpl.java:[line 341] ...... M C NP: Method call in com.mysql.jdbc.DatabaseMetaData.getInstance(ConnectionImpl, String) passes null for unconditionally dereferenced parameter of new DatabaseMetaData(ConnectionImpl, String) Method invoked at DatabaseMetaData.java:[line 632] ...... H S SQL: Method com.mysql.jdbc.DatabaseMetaData.getColumnPrivileges(String, String, String, String) passes a nonconstant String to an execute method on an SQL statement At DatabaseMetaData.java:[line 2156] H S SQL: Method com.mysql.jdbc.DatabaseMetaData.getTablePrivileges(String, String, String) passes a nonconstant String to an execute method on an SQL statement At DatabaseMetaData.java:[line 4638] M S SQL: Method com.mysql.jdbc.ConnectionImpl.setSessionVariables() passes a nonconstant String to an execute method on an SQL statement At ConnectionImpl.java:[line 5074] ...... M M IS: Inconsistent synchronization of com.mysql.jdbc.CallableStatement.outputParameterResults; locked 50% of time Unsynchronized access at CallableStatement.java:[line 1948] M M IS: Inconsistent synchronization of com.mysql.jdbc.StatementImpl.wasCancelledByTimeout; locked 83% of time Unsynchronized access at PreparedStatement.java:[line 1756] ...... Warnings generated: 154 ......Learning to read the output of FindBugs takes a little time. What I do is just work through a certain error or warning when I’m bored or frustrated with the code that I should be writing. It’s my procrastination work. :-) I have only picked out the things that as a developer strike me as problematic: “passes null for unconditionally dereferenced parameter”. Eek. NullPointerException anyone? Of course, this could well be test code, or even unused code that is still under development. How about this: “passes a nonconstant String to an execute method on an SQL statement”. Hmm. If left unchecked, this could be a vector for an SQL injection vulnerability. I said earlier that FindBugs does not require you to have access to the source code of an application to find bugs in it. Just for laughs, let’s have a look at one of the cornerstones of our Java EE application servers: The Oracle JDBC driver. $ findbugs -textui -effort:max ojdbc6.jar ...... M B Dm: oracle.sql.ConverterArchive.openArchiveforRead() invokes System.exit(...), which shuts down the entire virtual machine At ConverterArchive.java:[line 375] M B Dm: oracle.sql.ConverterArchive.closeArchiveforRead() invokes System.exit(...), which shuts down the entire virtual machine At ConverterArchive.java:[line 390] ...... M B ES: Comparison of String objects using == or != in oracle.jdbc.connector.OracleConnectionRequestInfo.equals(Object) At OracleConnectionRequestInfo.java:[line 104] ...... H C IL: There is an apparent infinite recursive loop in oracle.jdbc.rowset.OracleCachedRowSet.updateBlob(int, InputStream, long) At OracleCachedRowSet.java:[line 6365] H C IL: There is an apparent infinite recursive loop in oracle.jdbc.rowset.OracleCachedRowSet.updateClob(int, Reader, long) At OracleCachedRowSet.java:[line 6445] H C IL: There is an apparent infinite recursive loop in oracle.jdbc.rowset.OracleCachedRowSet.updateNClob(int, Reader, long) At OracleCachedRowSet.java:[line 6535] ...... Warnings generated: 1028 ......That driver produces no less than 1028 warnings. Wow. I’m not suggesting that the Oracle JDBC driver is actually a bad piece of code. I just find that it smells a little. :-) Oracle’s developers might want to have a crack at resolving the findbugs-reported warnings. There are many little performance and stability suggestions in there. And yes: findbugs checks for uses of System.gc(). PS. Please be aware that I used FindBugs 1.3.7. This version of FindBugs has a bug that causes it to generate false positives for cleaning up database resources. PPS. Who am I kidding: I do think that the Oracle driver is bad. Infinite loops? System.exit()? Please. Always help a … coder in need ;-) Byron Related Articles:Things Every Programmer Should Know 10 Tips for Proper Application Logging Laws of Software Design Java Best Practices Series 9 Tips on Surviving the Wild West Development Process...
xuggler-logo

Xuggler Tutorial: Frames Capture and Video Creation

Note: This is part of our “Xuggler Development Tutorials” series. So far in our Xuggler tutorials series we have performed an Introduction to Xuggler for Video Manipulation and we have discussed Transcoding and Media Modification. In this tutorial, we shall see how to decode video and capture frames, as well as how to create video from scratch. Let’s begin with decoding a video stream and capturing some frames at predefined time intervals. This can be used, for example, in order to make thumbnails of a media file. For this purpose, we will use again the MediaTool API, a high-level API for decoding, encoding and modifying video. The concept is to open the media file, loop through a specific video stream and at specific intervals capture the corresponding frame, convert it to an image and dump the binary contents into a file. Here is what the code for all these looks like: package com.javacodegeeks.xuggler;import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException;import javax.imageio.ImageIO;import com.xuggle.mediatool.IMediaReader; import com.xuggle.mediatool.MediaListenerAdapter; import com.xuggle.mediatool.ToolFactory; import com.xuggle.mediatool.event.IVideoPictureEvent; import com.xuggle.xuggler.Global;public class VideoThumbnailsExample { public static final double SECONDS_BETWEEN_FRAMES = 10;private static final String inputFilename = "c:/Java_is_Everywhere.mp4"; private static final String outputFilePrefix = "c:/snapshots/mysnapshot"; // The video stream index, used to ensure we display frames from one and // only one video stream from the media container. private static int mVideoStreamIndex = -1; // Time of last frame write private static long mLastPtsWrite = Global.NO_PTS; public static final long MICRO_SECONDS_BETWEEN_FRAMES = (long)(Global.DEFAULT_PTS_PER_SECOND * SECONDS_BETWEEN_FRAMES);public static void main(String[] args) {IMediaReader mediaReader = ToolFactory.makeReader(inputFilename);// stipulate that we want BufferedImages created in BGR 24bit color space mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR); mediaReader.addListener(new ImageSnapListener());// read out the contents of the media file and // dispatch events to the attached listener while (mediaReader.readPacket() == null) ;}private static class ImageSnapListener extends MediaListenerAdapter {public void onVideoPicture(IVideoPictureEvent event) {if (event.getStreamIndex() != mVideoStreamIndex) { // if the selected video stream id is not yet set, go ahead an // select this lucky video stream if (mVideoStreamIndex == -1) mVideoStreamIndex = event.getStreamIndex(); // no need to show frames from this video stream else return; }// if uninitialized, back date mLastPtsWrite to get the very first frame if (mLastPtsWrite == Global.NO_PTS) mLastPtsWrite = event.getTimeStamp() - MICRO_SECONDS_BETWEEN_FRAMES;// if it's time to write the next frame if (event.getTimeStamp() - mLastPtsWrite >= MICRO_SECONDS_BETWEEN_FRAMES) { String outputFilename = dumpImageToFile(event.getImage());// indicate file written double seconds = ((double) event.getTimeStamp()) / Global.DEFAULT_PTS_PER_SECOND; System.out.printf( "at elapsed time of %6.3f seconds wrote: %s\n", seconds, outputFilename);// update last write time mLastPtsWrite += MICRO_SECONDS_BETWEEN_FRAMES; }} private String dumpImageToFile(BufferedImage image) { try { String outputFilename = outputFilePrefix + System.currentTimeMillis() + ".png"; ImageIO.write(image, "png", new File(outputFilename)); return outputFilename; } catch (IOException e) { e.printStackTrace(); return null; } }}}This might seem a bit overwhelming, but it is really quite straightforward. Let me provide some details for you. We start by creating an IMediaReader from an input file. The media reader is used to read and decode media. Since we wish to manipulate the captures video frames as images, we use the setBufferedImageTypeToGenerate method to denote this. The reader opens up a media container, reads packets from it, decodes the data, and then dispatches information about the data to any registered IMediaListener objects. Here is where our custom class, ImageSnapListener, comes into play. Our listener extends the MediaListenerAdapter, which is an adapter (provides empty methods) implementing the IMediaListener interface. Objects that implement this interface are notified about events generated during the video processing. We only care about handling video events, thus we only implement the IMediaListener.onVideoPicture method. Inside that we use the provided IVideoPictureEvent object to find what stream (video only) we are dealing with. Since we wish to capture frames in specific times, we have to mess a little with timestamps. First, we make sure we handle the very first frame by checking against the value of the Global.NO_PTS constant, which is a value that means no time stamp is set for a given object. Then, if the minimum elapsed time has passes, we capture the frame by invoking the IVideoPictureEvent.getImage method, which returns the underlying BufferedImage. Note that we are talking about elapsed video time and not “real time”. We then dump the image data to a file in PNG format using the ImageIO.write utility method. Finally, we update the last write time. Let’s run this application in order to see the results. As input file, I am using an old Sun commercial proclaiming that “Java is Everywhere”. I have downloaded locally the MP4 version provided. Here is what the output console will look like: at elapsed time of 0.000 seconds wrote: c:/snapshots/mysnapshot1298228503292.png at elapsed time of 10.010 seconds wrote: c:/snapshots/mysnapshot1298228504014.png at elapsed time of 20.020 seconds wrote: c:/snapshots/mysnapshot1298228504463.png … at elapsed time of 130.063 seconds wrote: c:/snapshots/mysnapshot1298228509454.png at elapsed time of 140.007 seconds wrote: c:/snapshots/mysnapshot1298228509933.png at elapsed time of 150.017 seconds wrote: c:/snapshots/mysnapshot1298228510379.png The total video time is about 151 seconds so we capture 16 frames. Here is what the captured images look like in my folder:OK, that’s about it for making video thumbnails. Let’s now see how to create a video from scratch. As input, we will use sequential snapshots from our desktop. This can be used for a rudimentary screen recording application. In order to create video, we will have to take a bit more low level approach in comparison to the MediaTool API that we have seen so far. Don’t worry though, it is not going to be complicated. The main idea is that we create a media writer, add some stream information to it, encode our media (the screenshot images), and close the writer. Let’s see the code used to achieve this: package com.javacodegeeks.xuggler;import java.awt.AWTException; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.util.concurrent.TimeUnit;import com.xuggle.mediatool.IMediaWriter; import com.xuggle.mediatool.ToolFactory; import com.xuggle.xuggler.ICodec;public class ScreenRecordingExample { private static final double FRAME_RATE = 50; private static final int SECONDS_TO_RUN_FOR = 20; private static final String outputFilename = "c:/mydesktop.mp4"; private static Dimension screenBounds;public static void main(String[] args) {// let's make a IMediaWriter to write the file. final IMediaWriter writer = ToolFactory.makeWriter(outputFilename); screenBounds = Toolkit.getDefaultToolkit().getScreenSize();// We tell it we're going to add one video stream, with id 0, // at position 0, and that it will have a fixed frame rate of FRAME_RATE. writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4, screenBounds.width/2, screenBounds.height/2);long startTime = System.nanoTime(); for (int index = 0; index < SECONDS_TO_RUN_FOR * FRAME_RATE; index++) { // take the screen shot BufferedImage screen = getDesktopScreenshot();// convert to the right image type BufferedImage bgrScreen = convertToType(screen, BufferedImage.TYPE_3BYTE_BGR);// encode the image to stream #0 writer.encodeVideo(0, bgrScreen, System.nanoTime() - startTime, TimeUnit.NANOSECONDS);// sleep for frame rate milliseconds try { Thread.sleep((long) (1000 / FRAME_RATE)); } catch (InterruptedException e) { // ignore } } // tell the writer to close and write the trailer if needed writer.close();} public static BufferedImage convertToType(BufferedImage sourceImage, int targetType) { BufferedImage image;// if the source image is already the target type, return the source image if (sourceImage.getType() == targetType) { image = sourceImage; } // otherwise create a new image of the target type and draw the new image else { image = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), targetType); image.getGraphics().drawImage(sourceImage, 0, 0, null); }return image; } private static BufferedImage getDesktopScreenshot() { try { Robot robot = new Robot(); Rectangle captureSize = new Rectangle(screenBounds); return robot.createScreenCapture(captureSize); } catch (AWTException e) { e.printStackTrace(); return null; } }}We start by creating an IMediaWriter from a given output file. This class encodes and decodes media, handling both audio and video streams. Xuggler guesses the output format from the file name extension (in our case MP4) and sets some default values appropriately. We then use the addVideoStream method to add a new video stream, providing its index, the codec type used (MPEG-4 here) and the video dimensions. The dimensions are set equal to half of the screen’s dimensions in this example. Then we execute a loop that runs for a number of times equal to the desired frame rate multiplied with the desired running time. Inside the loop, we generate a screen snapshot as described in the Java2D: Screenshots with Java article. We retrieve the screenshot as a BufferedImage and convert it to the appropriate type (TYPE_3BYTE_BGR) if it is not already there. Next, we encode the image to the video stream using the IMediaWriter.encodeVideo method. We provide the stream index, the image, the elapsed video time and the time unit. Then, we sleep for the appropriate number of time, depending on the desired frame rate. When the loop is over, we close the writer and write the trailer if necessary, depending on the video format (this is done automatically by Xuggler). If we execute the application, a video will be created which has recorded your desktop actions. Here is a still image of mine while browsing the JavaCodeGeeks site:That’s it guys, yet another Xuggler tutorial describing how to capture video frames from an input file and how to generate video using desktop snapshots. As always, you can download the Eclipse project created for this tutorial. Stay tuned for more Xuggler tutorials here at JavaCodeGeeks! And don’t forget to share! Related Articles:Introduction to Xuggler for Video Manipulation Xuggler Tutorial: Transcoding and Media Modification RTMP To RTSP re-stream using wowza and xuggler...
software-development-2-logo

Selecting a new programming language to learn

Looking to start learning a programming language besides Java? Our JCG partner Brian Du Preez from Zen in the art of IT provides a relatively extensive analysis on the factors one should take into account when facing the “what’s beyond Java” question! Let’s see what he has to say: (NOTE: The original post has been slightly edited to improve readability) I have been itching to learn a new language, but being a Java freak, I always end up convincing myself to spend the time and effort discovering, investigating or playing with something in the Java open source stable, Spring, Hadoop, Joda Time, Hibernate, Maven, Hazelcast, EhCache etc etc. Developing in Java these days is almost purely about knowing and wiring together frameworks, which is both a good and a bad thing (as well as a topic for another day). Now to get myself to not redirect the “new language” energy into Y.A.F (yet another framework) I decided to give the languages out there a proper look and see which would be the best fit and most beneficial to my work, marketability and just general ‘IT Zen’. So what do I require from a language:IDE… my number 1 thing is an IDE, if there isn’t a decent IDE for a language it is frankly not worth the time and effort. I don’t see myself as a “scientist” where I feel the need to cause myself pain and inconvenience to be “pure”. I want a comfortable productive working environment, and VI or Notepad with a command line utility ain’t it. Established… Every couple of years, someone somewhere tries to define some new language, and most of those die in obscurity; for example brainf*** or anything listed on Esolang. Popular / In Demand… As with most things, popularity is good, it means: open source community, support and most importantly jobs. If you ever want to see the current popularity of a language Tiobe is the site to visit.So who are the contenders out there? Based on Feb 2011 Tiobe index: Java is still the no.1 most popular, it has awesome IDEs and it’s been around for just more than 15 years (January 23, 1996), but thankfully I know Java reasonably well :)… so moving right along… To narrow down the list quickly, I won’t look at any languages that are losing popularity, for obvious reasons, so from the top 20 on the Tiobe list that excludes: C, C++, PHP, VB, JS, Perl, Ruby, Delphi, Go. (C, C++, PHP, VB, JS, Perl, Ruby, Delphi, Go.) Which leaves behind: Python, C#, Objective-C, Lisp, NXT-G, Ada, Pascal, Lua, RPG Now there is a line between established and old, I am going to make a call that could offend some people and say Pascal and RPG are just old. (Pascal, RPG) Ada, don’t know much about it, after reading the ADA overview, it seems okay, going to exclude it based on popularity. (Ada) Lua, from a quick read it is a scripting language. (Lua) NXT-G has something to do with lego or some robotics, not very mainstream. (NXT-G) Lisp again like Ada, at first glace seems fine, just not popular enough. (Lisp) Then there are the “New, built on other platforms” functional languages: Scala, F#, Clojure. Although very temping being on the bleeding edge, it’s not all that profitable or marketable yet. I’ll give them some time to standardize, settle down and see if they are widely adopted. They do appeal greatly to my inner geek, so will always be keeping an eye on them. So this leaves me with: Python, C#, Objective-C, (and Java). Straight away based on the above list we can Tick: IDE, Established and Popular / In demand. We all know they have decent IDEs: Eclipse, XCode, Visual Studio, (IntelliJ and Netbeans). They have also been around and are well known. Now looking at number of jobs: Found a site (Simply hired) with a graph displays the percentage of jobs with your search terms anywhere in the job listing. Since June 2009, the following has occurred: Python jobs increased 72% C# jobs increased 77% Objective-c jobs increased 268% Java jobs increased 76%Python, C#, Objective-c, Java trends Python jobs | C# jobs | Objective-c jobs | Java jobsWith the recent boom of iPads and iPhones the Objective-C percentage is not all that surprising. I do have a problem with Apple, Objective-C and XCode and that problem is you need a Mac to run it. Once you start down that road you end up having to change everything to Apple, and I am not ready to do that. So for now I am going to drop Objective-C out of the running. Although if I ever do buy into the whole Apple thing, this will got back to the list. Leaving me with Python and C#, looking at their salaries compared with Java: (Data from Payscale). US Data Java PayScale – Java Skill Salary, Average Salaries by Years Experience Median Salary by Years Experience – Skill: Java (United States)CAREER TOOLS: Salary Calculator, Career Path Tool,Cost of Living Calculator, Meeting MiserPython PayScale – Python Skill Salary, Average Salaries by Years Experience Median Salary by Years Experience – Skill: Python (United States)CAREER TOOLS: Salary Calculator, Career Path Tool,Cost of Living Calculator, Meeting MiserC# PayScale – C# Skill Salary, Average Salaries by Years Experience Median Salary by Years Experience – Skill: C# (United States)CAREER TOOLS: Salary Calculator, Career Path Tool,Cost of Living Calculator, Meeting MiserSouth Africa Data Java PayScale – Java Skill Salary, Average Salaries by Years Experience Median Salary by Years Experience – Skill: Java (South Africa)CAREER TOOLS: Salary Calculator, Career Path Tool,Cost of Living Calculator, Meeting MiserPython PayScale – Python Skill Salary, Average Salaries by Years Experience Median Salary by Years Experience – Skill: Python (South Africa)CAREER TOOLS: Salary Calculator, Career Path Tool,Cost of Living Calculator, Meeting MiserC# PayScale – C# Skill Salary, Average Salaries by Years Experience Median Salary by Years Experience – Skill: C# (South Africa)CAREER TOOLS: Salary Calculator, Career Path Tool,Cost of Living Calculator, Meeting MiserBased on the US data, I would have gone with Python, it’s not as popular as C# but the pay is slightly better, I would also get to keep using Eclipse (PyDev) and Spring, but as soon as I looked at the South African data, I realized something, Python is really not big here. I manually went searching for Python positions advertised.. and found a grand total of 2 and the salaries were not good. (Python) Leaving C# as the last language standing. It’s got Visual Studio (even a free version Visual Studio Express), It has proven itself over the last couple years, it’s out innovating Java at the moment, there’s a ton of jobs, a whole range of certifications and the salaries have closed the gap on Java. Seems quite a logical choice to me. To top it off, I have also used C# many years back, so it won’t not entirely new. Most of the successful Java open source projects (Spring, Hibernate etc etc) have been ported so all that knowledge is reusable, which also counted a little in my decision. Now I just need to stop working 12-14 hours a day, and I can focus on getting back to my Microsoft roots with little C# as a Java developer. Hopefully a couple months after that I can go through this process again, looking at Python, Objective-C, the mobile platforms (iOS, Android, windows) or maybe rather a concept change to functional with the likes of Clojure or Scala. Always help a … coder in need ;-) Byron Related Articles:Things Every Programmer Should Know 10 Tips for Proper Application Logging Laws of Software Design Java Best Practices Series 9 Tips on Surviving the Wild West Development ProcessTranslations: This article is translated to Serbo-Croatian language by WHGeeks. ...
Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books