All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.grid.io.impl.ConversationImpl Maven / Gradle / Ivy

There is a newer version: 5.6.0.Final
Show newest version
package org.drools.grid.io.impl;

import java.util.concurrent.atomic.AtomicInteger;

import org.drools.grid.io.Connector;
import org.drools.grid.io.Conversation;
import org.drools.grid.io.ConversationManager;
import org.drools.grid.io.IoWriter;
import org.drools.grid.io.Message;
import org.drools.grid.io.MessageReceiverHandler;

public class ConversationImpl
    implements
    Conversation {

    private Connector                       conn;
    private IoWriter                        writer;
    private String                          conversationId;
    private String                          senderId;
    private String                          recipientId;
    private AtomicInteger                   requestId;
    private RequestResponseDispatchListener dispathListener;
    private ConversationManager             conversationManager;

    private Message                         receivedMessage;

    public ConversationImpl(Connector conn, 
                            String conversationId,
                            String senderId,
                            String recipientId,
                            RequestResponseDispatchListener dispathListener,
                            IoWriter writer,
                            ConversationManager conversationManager) {
        this( conn,
              conversationId,
              senderId,
              recipientId,
              dispathListener,
              null,
              writer,
              conversationManager );
    }

    public ConversationImpl(Connector conn,
                            String conversationId,
                            String senderId,
                            String recipientId,
                            RequestResponseDispatchListener dispathListener,
                            Message receivedMessage,
                            IoWriter writer,
                            ConversationManager conversationManager) {
        this.conn = conn;
        this.conversationId = conversationId;
        this.senderId = senderId;
        this.recipientId = recipientId;
        this.dispathListener = dispathListener;
        this.writer = writer;
        this.requestId = new AtomicInteger();
        this.receivedMessage = receivedMessage;
        this.conversationManager = conversationManager;
    }

    public void respond(Object body) {
        Message msg = new MessageImpl( this.conversationId,
                                       this.senderId,
                                       recipientId,
                                       -1,
                                       receivedMessage.getRequestId(),
                                       body );

        writer.write( msg );
    }
    
    public void respondError(Throwable throwable) {
        Message msg = new ExceptionMessage( this.conversationId,
                                       this.senderId,
                                       recipientId,
                                       -1,
                                       receivedMessage.getRequestId(),
                                       throwable );

        writer.write( msg );
    }

    public void sendMessage(Object body,
                                MessageReceiverHandler handler) {
        int requestId = -1;
        if ( handler != null ) {
            requestId = this.requestId.getAndIncrement();
        }
        Message msg = new MessageImpl( this.conversationId,
                                       this.senderId,
                                       recipientId,
                                       requestId,
                                       -1,
                                       body );

        this.dispathListener.addMessageReceiverHandler( requestId,
                                                        handler );
        writer.write( msg );
    }

    public void endConversation() {
        if ( conn != null && conn.isOpen() ) {
            conn.close();
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy