public boolean offer(T element) { try { _zkClient.createPersistentSequential(_root + "/" + ELEMENT_NAME + "-", element); } catch (Exception e) { throw ExceptionUtil.convertToRuntimeException(e); } return true; }
public T poll() { while (true) { Element<T> element = getFirstElement(); if (element == null) { return null; } try { _zkClient.delete(element.getName()); return element.getData(); } catch (ZkNoNodeException e) { // somebody else picked up the element first, so we have to // retry with the new first element } catch (Exception e) { throw ExceptionUtil.convertToRuntimeException(e); } } }
@SuppressWarnings("unchecked") private Element<T> getFirstElement() { try { while (true) { List<String> list = _zkClient.getChildren(_root); if (list.size() == 0) { return null; } String elementName = getSmallestElement(list); try { return new Element<T>( _root + "/" + elementName, (T) _zkClient.readData(_root + "/" + elementName)); } catch (ZkNoNodeException e) { // somebody else picked up the element first, so we have to // retry with the new first element } } } catch (Exception e) { throw ExceptionUtil.convertToRuntimeException(e); } }