~trond-norbye/gearman-java/checkstyle_support

« back to all changes in this revision

Viewing changes to src/org/gearman/common/GearmanJobServerSession.java

  • Committer: Eric Lambert
  • Date: 2009-07-19 18:51:04 UTC
  • mfrom: (1.1.13 perf)
  • Revision ID: eric.d.lambert@gmail.com-20090719185104-244ikz9qyosjgjjw
merge from perf branch: removed sleep in workerimpl work loop and move control of session selector's interest ops into session

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
import java.util.concurrent.TimeoutException;
15
15
import java.util.logging.Level;
16
16
import java.util.logging.Logger;
17
 
import org.gearman.client.GearmanClientImpl;
18
17
import org.gearman.client.GearmanIOEventListener;
19
18
 
20
19
public class GearmanJobServerSession
48
47
        return DESCRIPTION;
49
48
    }
50
49
 
51
 
    public void initSession(Selector sel, int mask,
52
 
            GearmanSessionEventHandler handler)
 
50
    public void initSession(Selector sel, GearmanSessionEventHandler handler)
53
51
            throws IllegalStateException, IOException {
54
52
        if (isInitialized()) {
55
53
            throw new IllegalStateException("A session can not be " +
57
55
        }
58
56
        connection.open();
59
57
        packetsToWrite = new LinkedList<GearmanPacket>();
60
 
        sessionSelectionKey = connection.registerSelector(sel, mask);
 
58
        sessionSelectionKey = connection.registerSelector(sel,
 
59
                SelectionKey.OP_READ);
61
60
        this.responseHandler = handler;
62
61
        newTaskList = new LinkedList<GearmanTask>();
63
62
        tasksAwaitingAckList = new LinkedList<GearmanTask>();
151
150
 
152
151
        newTaskList.add(task);
153
152
        packetsToWrite.add(task.getRequestPacket());
 
153
        sessionSelectionKey.interestOps(sessionSelectionKey.interestOps() |
 
154
                SelectionKey.OP_WRITE);
154
155
        LOG.log(Level.FINER, "Session " + this + " is now handling " +
155
156
                "the task " + task);
156
157
    }
184
185
                handleSessionEvent(new GearmanSessionEvent(p, this));
185
186
            }
186
187
        }
 
188
        if (!sessionHasDataToWrite()) {
 
189
            sessionSelectionKey.interestOps(SelectionKey.OP_READ);
 
190
        }
187
191
 
188
192
        while (canRead()) {
189
193
            GearmanPacket p = connection.read();
240
244
        return tasksAwaitingAckList.size() + newTaskList.size();
241
245
    }
242
246
 
 
247
    public boolean sessionHasDataToWrite() {
 
248
        if (connection == null) {
 
249
            return false;
 
250
        }
 
251
        return packetsToWrite.isEmpty() ? connection.hasBufferedWriteData() : true;
 
252
    }
 
253
 
243
254
    private void handleReqSessionEvent(GearmanSessionEvent event) {
244
255
        GearmanPacket p = event.getPacket();
245
256
        GearmanTask t = newTaskList.remove();
395
406
        return connection.canRead();
396
407
    }
397
408
 
398
 
    private boolean sessionHasDataToWrite() {
399
 
        if (connection == null) {
400
 
            return false;
401
 
        }
402
 
        return packetsToWrite.isEmpty() ?
403
 
            connection.hasBufferedWriteData() : true;
404
 
    }
405
409
}