View Javadoc

1   /**********************************************************************
2    * EJAsyncWorker.java
3    * created on 12.11.2006 by netseeker
4    * $Id: EJAsyncWorker.java,v 1.2 2006/11/20 22:25:04 netseeker Exp $
5    * $Log: EJAsyncWorker.java,v $
6    * Revision 1.2  2006/11/20 22:25:04  netseeker
7    * *** empty log message ***
8    *
9    * Revision 1.1  2006/11/12 20:34:43  netseeker
10   * *** empty log message ***
11   *
12   *
13   * ====================================================================
14   *
15   *  Copyright 2005-2006 netseeker aka Michael Manske
16   *
17   *  Licensed under the Apache License, Version 2.0 (the "License");
18   *  you may not use this file except in compliance with the License.
19   *  You may obtain a copy of the License at
20   *
21   *      http://www.apache.org/licenses/LICENSE-2.0
22   *
23   *  Unless required by applicable law or agreed to in writing, software
24   *  distributed under the License is distributed on an "AS IS" BASIS,
25   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26   *  See the License for the specific language governing permissions and
27   *  limitations under the License.
28   * ====================================================================
29   *
30   * This file is part of the EJOE framework.
31   * For more information on the author, please see
32   * <http://www.manskes.de/>.
33   *
34   *********************************************************************/
35  package de.netseeker.ejoe;
36  
37  import java.io.IOException;
38  
39  /***
40   * A simple worker thread which can be used to realize 
41   * asynchronous client requests based on a callback pattern
42   * @author netseeker
43   * @since 0.3.9.1
44   */
45  public class EJAsyncWorker implements Runnable
46  {
47      private long            _ident;
48  
49      private Object          _request;
50  
51      private EJClient        _client;
52  
53      private EJAsyncCallback _callback;
54  
55      /***
56       * @param client
57       * @param request
58       * @param callback
59       * @param ident
60       */
61      public EJAsyncWorker(final EJClient client, final Object request, final EJAsyncCallback callback, final long ident)
62      {
63          this._client = client;
64          this._request = request;
65          this._ident = ident;
66          this._callback = callback;
67      }
68  
69      /*
70       * (non-Javadoc)
71       * 
72       * @see java.lang.Runnable#run()
73       */
74      public void run()
75      {
76          if ( this._client != null && this._callback != null )
77          {
78              Object result = null;
79  
80              try
81              {
82                  result = _client.execute( this._request );
83              }
84              catch ( IOException e )
85              {
86                  this._callback.onErrorOccured( this._ident, e );
87              }
88  
89              this._callback.onRequestProcessed( this._ident, result );
90          }
91      }
92  }