Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
- Improved Messages ids for concurrent scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
esteban-aliverti committed Aug 20, 2012
1 parent 763b81e commit f5d6141
Showing 1 changed file with 14 additions and 12 deletions.
Expand Up @@ -22,6 +22,8 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import org.drools.mas.ACLMessage;
import org.drools.mas.Act;
import org.drools.mas.AgentID;
Expand Down Expand Up @@ -60,9 +62,9 @@
*/
public class ACLMessageFactory implements Serializable {

private static long idCounter = 0;
private static long convoCounter = 0;

private static AtomicLong idCounter = new AtomicLong();
private static AtomicLong convCounter = new AtomicLong();
private static ACLMessageFactory instance;

public static ACLMessageFactory getInstance(){
Expand All @@ -72,12 +74,12 @@ public static ACLMessageFactory getInstance(){
return instance;
}

private String newId() {
return "" + (idCounter++);
private long newId() {
return idCounter.incrementAndGet();
}

private String newConversationId() {
return "" + (convoCounter++);
private long newConversationId() {
return convCounter.incrementAndGet();
}
private Encodings defaultEncoding = Encodings.XML;

Expand All @@ -94,20 +96,20 @@ public ACLMessageFactory(Encodings defEncoding) {
}

public ACLMessage newMessage() {
return new ACLMessage( newId() );
return new ACLMessage( UUID.randomUUID().toString()+"-"+newId() );
}

protected ACLMessage newMessage( String sender, String receiver ) {

ACLMessage msg = new ACLMessage();

msg.setConversationId( newConversationId() );

AgentID senderAgent = new AgentID();
senderAgent.setName( sender );
msg.setSender( senderAgent );

msg.setId( newId() + senderAgent.toString() );
msg.setConversationId( senderAgent.toString() +"-"+ newConversationId());

msg.setId( senderAgent.toString() +"-"+ newId());

List<AgentID> recSet = msg.getReceiver();
AgentID receiverAgent = new AgentID();
Expand All @@ -121,7 +123,7 @@ protected ACLMessage newMessage( String sender, String receiver ) {

protected ACLMessage createReply(ACLMessage inMsg, AgentID sender) {

ACLMessage msg = new ACLMessage(newId());
ACLMessage msg = newMessage();
msg.setEncoding(inMsg.getEncoding());
msg.setSender(sender);

Expand Down

0 comments on commit f5d6141

Please sign in to comment.