Asked  7 Months ago    Answers:  5   Viewed   40 times

One of the first things I've learned about Java EE development is that I shouldn't spawn my own threads inside a Java EE container. But when I come to think about it, I don't know the reason.

Can you clearly explain why it is discouraged?

I am sure most enterprise applications need some kind of asynchronous jobs like mail daemons, idle sessions, cleanup jobs etc.

So, if indeed one shouldn't spawn threads, what is the correct way to do it when needed?



It is discouraged because all resources within the environment are meant to be managed, and potentially monitored, by the server. Also, much of the context in which a thread is being used is typically attached to the thread of execution itself. If you simply start your own thread (which I believe some servers will not even allow), it cannot access other resources. What this means, is that you cannot get an InitialContext and do JNDI lookups to access other system resources such as JMS Connection Factories and Datasources.

There are ways to do this "correctly", but it is dependent on the platform being used.

The commonj WorkManager is common for WebSphere and WebLogic as well as others

More info here

And here

Also somewhat duplicates this one from this morning

UPDATE: Please note that this question and answer relate to the state of Java EE in 2009, things have improved since then!

Tuesday, June 1, 2021
answered 7 Months ago

This question pops up once in a while.

As per the spec it's not authorized. The best page to look at is this one: Q/A: J2EE Restrictions

That said, there are ways to spawn threads, especiall in Weblogic with the WorkManager.

See these questions:

  • How can an EJB parallelize a long, CPU intensive process?
  • Why spawning threads in J2EE container is discouraged?
  • J2EE programmers do not write to files

The fact that the first one targets EJB shouldn't matter that much, and the last one about access to file system is about general restrictions.

Hope it helps.

Wednesday, June 2, 2021
answered 7 Months ago
0 == ''

The left operand is of the type Number.
The right operand is of the type String.

In this case, the right operand is coerced to the type Number:

0 == Number('')

which results in

0 == 0

From the Abstract Equality Comparison Algorithm (number 4):

If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).


Tuesday, July 13, 2021
answered 5 Months ago

Replying to the last part of your question:

Are there situations where you would want the string 'false' to represent true?

Let's consider I am testing for empty strings in my user input. To achieve that, I issue:

if (!theInput) {
    // Do something.

Now do I want that condition to be true if the user enters false in the text box? Of course, I don't.

Tuesday, August 24, 2021
answered 4 Months ago

I am getting the error (...) javax.naming.NameNotFoundException: greetJndi not bound

This means that nothing is bound to the jndi name greetJndi, very likely because of a deployment problem given the incredibly low quality of this tutorial (check the server logs). I'll come back on this.

Is there any specific directory structure to deploy in JBoss?

The internal structure of the ejb-jar is supposed to be like this (using the poor naming conventions and the default package as in the mentioned link):

    ├── ejb-jar.xml
    └── jboss.xml

But as already mentioned, this tutorial is full of mistakes:

  • there is an extra character (<enterprise-beans>] <-- HERE) in the ejb-jar.xml (!)
  • a space is missing after PUBLIC in the ejb-jar.xml and jboss.xml (!!)
  • the jboss.xml is incorrect, it should contain a session element instead of entity (!!!)

Here is a "fixed" version of the ejb-jar.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "">

And of the jboss.xml:

<?xml version="1.0"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.2//EN" "">

After doing these changes and repackaging the ejb-jar, I was able to successfully deploy it:

21:48:06,512 INFO  [Ejb3DependenciesDeployer] Encountered deployment AbstractVFSDeploymentContext@5060868{vfszip:/home/pascal/opt/jboss-5.1.0.GA/server/default/deploy/greet.jar/}
21:48:06,534 INFO  [EjbDeployer] installing bean: ejb/#greetBean,uid19981448
21:48:06,534 INFO  [EjbDeployer]   with dependencies:
21:48:06,534 INFO  [EjbDeployer]   and supplies:
21:48:06,534 INFO  [EjbDeployer]    jndi:greetJndi
21:48:06,624 INFO  [EjbModule] Deploying greetBean
21:48:06,661 WARN  [EjbModule] EJB configured to bypass security. Please verify if this is intended. Bean=greetBean Deployment=vfszip:/home/pascal/opt/jboss-5.1.0.GA/server/default/deploy/greet.jar/
21:48:06,805 INFO  [ProxyFactory] Bound EJB Home 'greetBean' to jndi 'greetJndi'

That tutorial needs significant improvement; I'd advise from staying away from

Wednesday, September 15, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :