last day (15 days later) » 

8:31 PM
2
A: How to get equals and hashcode to work with Java RMI?

wickstopherYou should try using instanceof instead of the getClass() method. if (obj == null || !(obj instanceof MyRemote)) { ... } This is, at the very least, more efficient and may be the cause of your issue. You might also want to consider simplifying your logic a bit. Right now, you have 3 retur...

 
RTF
It didn't work unfortunately
 
Can you post the code where you're actually calling the equals (or contains) method?
As per the code you just posted, the call to contains should be returning false (as you say it is), as you never added the new instance to your HashSet.
 
RTF
sorry, just pseudocode. It will be in the set by the time the comparison is made. I'll try to post something more complete
 
Looking at your updated code. So, why are you checking for whether or not an object is an instance of ChatClientImpl in the equals method for class MyRemoteImpl?
 
RTF
;) thats not the update, I accidentally put that in when I updated with the instanceof line. That's my actual class name, I've just been using MyRemote as a general example. I'll post something more complete in a few minutes, I'm just trying to filter out anything irrelevant to the issue to make it as easy as possible to understand
 
8:32 PM
Sounds good. I tested the scenario out in (and admittedly non-RMI) chunk of code on my machine, and i'm not having any problems. Take a look at the simplified equals method I added to my answer, as well.
(moved the discussion out of the comments, btw)
 
RTF
Some more complete code (it's a toy example for a chat application that loosely follows a peer-to-peer architecture):

private void sendMessage(MessageImpl msg) {
Iterator<ChatClient> clientIterator;
try {
// list of all known clients fetched from a server via RMI call
clientIterator = server.getClients().iterator();
} catch (RemoteException e) {
// log the error...
return;
}
// clients that have already seen the message
Set<ChatClient> alreadyPassedThru = msg.getRecipients();
boolean messageSent = false;
I've never done private chat before, is there code formatting ??
 
See my most recent edit. You were doing this:
MyRemote other = (MyRemoteImpl) obj;
where you should've been doing this:
MyRemoteImpl other = (MyRemoteImpl) obj;
 
RTF
Sorry, no, that was just me typing the example badly, what I have for equals is this:
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || !(obj instanceof ChatClientImpl)) {
return false;
}
ChatClientImpl other = (ChatClientImpl) obj;
return this.clientName.equals(other.clientName);}
 
tough to help if you're posting stuff with problems your original code didn't have :)
 
RTF
I know, sorry, I was trying to simplify it as much as possible to make it easier to understand
 
8:39 PM
np, it happens
you should go back and tidy up your question code, though. i'll take a look at what you sent me
i'm not seeing in here where the ChatClient is being added to the alreadyPassedThru set
also, you know that you don't need to use iterators anymore, right?
you can just do:
for (ChatClient client : server.getClients()) {
try {
    ....
....
}
 
RTF
It's not in that function, I'll update the post with the function - really (about the iterators) I've had some bad experiences with altering lists while iterating (Java 6)
Java 7 or 8 only ?
 
hmm, maybe 7
you're not supposed to alter lists while using an iterator
so why are you using Java 6?! 8 Has been out for over a year!
 
RTF
No, I'm using Java 7 at the moment. Although I'm looking after a system at the moment that's still using 6, and it might be a pain in the ass to upgrade. It's a web environment running tomcat 6
Haven't checked out the Java 8 features at all yet, must take a look at the lambda stuff
 
8:56 PM
So, it looks like you are adding Strings (inMessage.id) to your HashSet
not MessageImpl objects
this.processedMessages.add(inMessage.id)
(from your updated code)
sobut you're not showing where the hashset is being declared
so it's tough to say
you also don't show your implementation of getRecipients() which would be helpful
but if you're trying to see if a HashSet of Strings contains a ChatClientImpl, well it won't, ever
 
RTF
The problem is with the 'alreadyPassedThru' hashset, I'll post the MessageImpl code...
Listen buddy, I don't want you to bust your ass too much on this. I'd like to understand why it's not working, but if I can't figure it out, I can still make the code work by replacing the set 'contains' call with a loop, like I mentioned in one of the edits. It just bugs me that my hashcode implementation doesn't take care of that automatically for me...
 
9:20 PM
Yeah, I'd run it through a debugger.
Honestly, at this point, your equals method should be working fine
i'd recommend simplifying it a bit as per my advicee
but i don't think that's where your problem is
it's more in your design, and you're going to have to take it from here
 
RTF
Ok, thanks for the help
 

last day (15 days later) »