     .flatMap (msg) =>
       subj = new Rx.Subject()
 
-      @persistenceService.unlockMessage msg
+      msg.persistence.unlockMessage msg
       .then (msg) ->
         subj.onNext(msg)
         subj.onCompleted()
-      .fail (error) ->
+      .fail (error) =>
         @errors.onNext {message: msg, error: error, processor: "Unlocking the message"}
         subj.onComplete()
       .done()

      console.info @toJSON(countOnly)
      console.info "+-------------------------------"

class BatchMessageService
  constructor: ->

  getMessages: ->
    # TODO
    return Rx.Observable.fromArray [
      {id: 3, resource: {typeId: "order", id: 1}, sequenceNumber: 3, name: "b"},
      {id: 1, resource: {typeId: "order", id: 1}, sequenceNumber: 1, name: "a"},
      {id: 2, resource: {typeId: "order", id: 1}, sequenceNumber: 2, name: "c"}]

class SphereService
  constructor: (@stats, options) ->

  getLastProcessedSequenceNumber: (resource) ->
    # TODO
    Q(0)

  reportSuccessfullProcessing: (msg) ->
    console.info "Success: ", msg
    # TODO
    Q(msg)


    .flatMap (msg) =>
      subj = new Rx.Subject()

      msg.persistence.reportSuccessfullProcessing msg
      .then (msg) ->
        subj.onNext(msg)
        subj.onCompleted()
      .fail (error) =>
        @errors.onNext {message: msg, error: error, processor: "Reporting successful processing"}
        subj.onComplete()
      .done()
