Categories
discuss

Is there a difference between BigDecimal(“0”) and BigDecimal.ZERO?

Either for comparisons or initialization of a new variable, does it make a difference which one of these you use?

I know that BigDecimal.ZERO is a 1.5 feature, so that’s a concern, but assuming I’m using 1.5 does it matter?

Thanks.

Answer

BigDecimal.ZERO is a predefined constant and therefore doesn’t have to be evaluated from a string at runtime as BigDecimal("0") would be. It will be faster and won’t require creation of a new object.

If your code needs to run on pre-1.5, then you can use the (much maligned) Singleton pattern to create an object equivalent to BigDecimal.ZERO. The first time it is used, it would call BigDecimal("0") to create a zero object, and return that object on subsequent calls. Otherwise, if your code is running on a 1.5 system, your singleton object can just return BigDecimal.ZERO with no runtime penalty.

Categories
discuss

What’s the best way to share business object instances between Java web apps using JBoss and Spring?

We currently have a web application loading a Spring application context which instantiates a stack of business objects, DAO objects and Hibernate. We would like to share this stack with another web application, to avoid having multiple instances of the same objects.

We have looked into several approaches; exposing the objects using JMX or JNDI, or using EJB3.

The different approaches all have their issues, and we are looking for a lightweight method.

Any suggestions on how to solve this?

Edit: I have received comments requesting me to elaborate a bit, so here goes:

The main problem we want to solve is that we want to have only one instance of Hibernate. This is due to problems with invalidation of Hibernate’s 2nd level cache when running several client applications working with the same datasource. Also, the business/DAO/Hibernate stack is growing rather large, so not duplicating it just makes more sense.

First, we tried to look at how the business layer alone could be exposed to other web apps, and Spring offers JMX wrapping at the price of a tiny amount of XML. However, we were unable to bind the JMX entities to the JNDI tree, so we couldn’t lookup the objects from the web apps.

Then we tried binding the business layer directly to JNDI. Although Spring didn’t offer any method for this, using JNDITemplate to bind them was also trivial. But this led to several new problems: 1) Security manager denies access to RMI classloader, so the client failed once we tried to invoke methods on the JNDI resource. 2) Once the security issues were resolved, JBoss threw IllegalArgumentException: object is not an instance of declaring class. A bit of reading reveals that we need stub implementations for the JNDI resources, but this seems like a lot of hassle (perhaps Spring can help us?)

We haven’t looked too much into EJB yet, but after the first two tries I’m wondering if what we’re trying to achieve is at all possible.

To sum up what we’re trying to achieve: One JBoss instance, several web apps utilizing one stack of business objects on top of DAO layer and Hibernate.

Best regards,

Nils

Answer

Are the web applications deployed on the same server?

I can’t speak for Spring, but it is straightforward to move your business logic in to the EJB tier using Session Beans.

The application organization is straight forward. The Logic goes in to Session Beans, and these Session Beans are bundled within a single jar as an Java EE artifact with a ejb-jar.xml file (in EJB3, this will likely be practically empty).

Then bundle you Entity classes in to a seperate jar file.

Next, you will build each web app in to their own WAR file.

Finally, all of the jars and the wars are bundled in to a Java EE EAR, with the associated application.xml file (again, this will likely be quite minimal, simply enumerating the jars in the EAR).

This EAR is deployed wholesale to the app server.

Each WAR is effectively independent — their own sessions, there own context paths, etc. But they share the common EJB back end, so you have only a single 2nd level cache.

You also use local references and calling semantic to talk to the EJBs since they’re in the same server. No need for remote calls here.

I think this solves quite well the issue you’re having, and its is quite straightforward in Java EE 5 with EJB 3.

Also, you can still use Spring for much of your work, as I understand, but I’m not a Spring person so I can not speak to the details.

Categories
discuss

What is the “Operation Aborted” error in Internet Explorer?

I recently added JQuery’s date-picker control to a project. In Internet Exploder, I get the following error message:

Internet Explorer cannot open the
Internet site

http://localhost/

Operation aborted

What is causing this problem?

Answer

There was a related question earlier today:

Operation Aborted Error in IE

This is a common problem.

It occurs in IE when a script tries to modify the DOM before the page is finished loading.

Take a look at what sort of scripts are executing. You’ll find that something is getting started before the page is finished loading. You can use the window.onload event to correct the problem (or one of the onDomReady library functions).

Categories
discuss

Read XLSX file in Java [closed]

I need to read an Excel 2007 XLSX file in a Java application. Does anyone know of a good API to accomplish this task?

Answer

AFAIK there are no xlsx-libraries available yet. But there are some for old xls:

One library is jxls which internally uses the already mentioned POI.

2 other links: Handle Excel files, Java libraries to read and write Excel XLS document files.

Categories
discuss

How to format a duration in java? (e.g format H:MM:SS)

I’d like to format a duration in seconds using a pattern like H:MM:SS. The current utilities in java are designed to format a time but not a duration.

Answer

If you’re using a version of Java prior to 8… you can use Joda Time and PeriodFormatter. If you’ve really got a duration (i.e. an elapsed amount of time, with no reference to a calendar system) then you should probably be using Duration for the most part – you can then call toPeriod (specifying whatever PeriodType you want to reflect whether 25 hours becomes 1 day and 1 hour or not, etc) to get a Period which you can format.

If you’re using Java 8 or later: I’d normally suggest using java.time.Duration to represent the duration. You can then call getSeconds() or the like to obtain an integer for standard string formatting as per bobince’s answer if you need to – although you should be careful of the situation where the duration is negative, as you probably want a single negative sign in the output string. So something like:

public static String formatDuration(Duration duration) {
    long seconds = duration.getSeconds();
    long absSeconds = Math.abs(seconds);
    String positive = String.format(
        "%d:%02d:%02d",
        absSeconds / 3600,
        (absSeconds % 3600) / 60,
        absSeconds % 60);
    return seconds < 0 ? "-" + positive : positive;
}

Formatting this way is reasonably simple, if annoyingly manual. For parsing it becomes a harder matter in general… You could still use Joda Time even with Java 8 if you want to, of course.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..