I looking at one EJB sample from the Java EE 6 tutorial. In This example, I want to know if I could just use @Singleton instead of @Stateless ?
package converter.ejb;
import java.math.BigDecimal;
import javax.ejb.*;
@Stateless
public class ConverterBean {
private BigDecimal yenRate = new BigDecimal("83.0602");
private BigDecimal euroRate = new BigDecimal("0.0093016");
public BigDecimal dollarToYen(BigDecimal dollars) {
BigDecimal result = dollars.multiply(yenRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}
public BigDecimal yenToEuro(BigDecimal yen) {
BigDecimal result = yen.multiply(euroRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}
}
Looks to me like a Util methods.
I could have use a static methods on this ConverterBean, if I wasn't using EJB.
And another question. I know that is a simple sample, but if I use this code from a servlet like in the sample, why using EJB only for this ?
Yes, I would say this is a bad use case for EJBs
at all. This methods should be definitely in some utility class declared as static methods. And for the Singleton, don't use if you don't need it (of course, you could make this class Singleton but what would you gain?). Keep your code as simple as possible.