• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

javax.jms.MessageListener and java.lang.Object

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a message driven bean that implemnts javax.jms.MessageListener, I created a "wait()" method for some reason. I checked the javax.jms.MessageListener API it seems it does not have anything to do with java.lang.Object class which comes with default "wait()" method. So is this OK ? Does it mess up any threading thing ?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wait() in Object is final so you can never override it. That means that no matter what class implements your listener, it will have the implementation from java.lang.Object. In other words, it's useless to define it in your interface.

Note: I assumed you created the wait() method in your listener interface because it's obviously not allowed to create it in any (abstract or non-abstract) class.
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bob, I didn't create it in the interface. As I said the interface is a standard javax.jms. nterface, how can I define it there ? I defined it in my Message Driven bean class and it gave me no error and works fine. Can you elaborate more about "obviously it is not allowed to ..." ? Do you mean it will not compile ? then why does my code compile and run OK ? please help explain.

Thanks.
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, my "wait()" method is NOT really doing any thread wait, it just hold the process for one minute. that's it.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's okay. As Rob already said, the wait() method in Object is final so you aren't overriding it. You are writing a completely new method which just happens to have the same name. In the very unlikely event that something is using your message listener object as a monitor for locking, it won't be calling your wait() method if it wants to wait on the monitor.

Although having said that, I would advise renaming the method anyway. Otherwise the next person who has to maintain your code might well have the same question. It is a private method, isn't it? You didn't mention that aspect of the question.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There can't be any wait() (or wait(long) or wait(long, int)) methods; not public, protected or private. Because the signature matches that of Object.wait() it will see it as an override, and you can't override with a lower visibility than public. Well, you can't override it at all.

As for the wait() in the interface, if you specify the method should wait one minute then that method is broken. Because the implementation is provided by Object.wait() and that will wait indefinitely until woken up or interrupted, that means it's not going to wait "one minute". So as said, use a different name or use a different parameter list, thereby overloading the method instead.
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually I just realized I defined it as

public static void wait(int seconds) {

....
}

1. I checked the java.lang.Object API, Since the method takes "int" as argument, it allows it to happen.

2. I agree with you that I should change the name of this method anyway !! Thanks. Now, the question is --- If I keep this

method, since it uses different method signature, would it really be a problem even I use it as "public static". Theoretically I don't think it is an issue because of the overloading principle. But just want to confirm. Again, I will change the name anyway but want to have some deeper understanding..
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. The wait() method and the wait(int) method are completely different methods. There is no way for the compiler to confuse the two. Of course it is possible for programmers to get confused...

And may I ask why this method is public? What outside object is going to be able to call that method?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this method is indeed an overloaded method there's no problem at all. But please TellTheDetails next time; your starting post mentioned wait() which is definitely not the same as wait(int).
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic