Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first attempt ot refactor the client push listener
- Loading branch information
Showing
3 changed files
with
166 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
client/src/main/java/com/orientechnologies/orient/client/remote/ORemoteConnectionPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.orientechnologies.orient.client.remote; | ||
|
||
import com.orientechnologies.common.concur.resource.OResourcePool; | ||
import com.orientechnologies.common.concur.resource.OResourcePoolListener; | ||
import com.orientechnologies.common.io.OIOException; | ||
import com.orientechnologies.common.log.OLogManager; | ||
import com.orientechnologies.orient.core.config.OContextConfiguration; | ||
import com.orientechnologies.orient.enterprise.channel.OChannel; | ||
import com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryAsynchClient; | ||
import com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryProtocol; | ||
import com.orientechnologies.orient.enterprise.channel.binary.OChannelListener; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Created by tglman on 01/10/15. | ||
*/ | ||
public class ORemoteConnectionPool extends ORemoteConnectionPushListener implements OResourcePoolListener<String, OChannelBinaryAsynchClient>, OChannelListener { | ||
|
||
|
||
private OResourcePool<String, OChannelBinaryAsynchClient> pool; | ||
|
||
public ORemoteConnectionPool(int iMaxResources) { | ||
pool = new OResourcePool<String, OChannelBinaryAsynchClient>(iMaxResources, this); | ||
} | ||
|
||
protected OChannelBinaryAsynchClient createNetworkConnection(String iServerURL, final OContextConfiguration clientConfiguration, Map<String, Object> iAdditionalArg) | ||
throws OIOException { | ||
if (iServerURL == null) | ||
throw new IllegalArgumentException("server url is null"); | ||
|
||
// TRY WITH CURRENT URL IF ANY | ||
try { | ||
OLogManager.instance().debug(this, "Trying to connect to the remote host %s...", iServerURL); | ||
|
||
final String serverURL; | ||
final String databaseName; | ||
int sepPos = iServerURL.indexOf("/"); | ||
if (sepPos > -1) { | ||
// REMOVE DATABASE NAME IF ANY | ||
serverURL = iServerURL.substring(0, sepPos); | ||
databaseName = iServerURL.substring(sepPos + 1); | ||
} else { | ||
serverURL = iServerURL; | ||
databaseName = null; | ||
} | ||
|
||
sepPos = serverURL.indexOf(":"); | ||
final String remoteHost = serverURL.substring(0, sepPos); | ||
final int remotePort = Integer.parseInt(serverURL.substring(sepPos + 1)); | ||
|
||
final OChannelBinaryAsynchClient ch = new OChannelBinaryAsynchClient(remoteHost, remotePort, databaseName, clientConfiguration, OChannelBinaryProtocol.CURRENT_PROTOCOL_VERSION, this); | ||
|
||
// REGISTER MYSELF AS LISTENER TO REMOVE THE CHANNEL FROM THE POOL IN CASE OF CLOSING | ||
ch.registerListener(this); | ||
|
||
return ch; | ||
|
||
} catch (OIOException e) { | ||
// RE-THROW IT | ||
throw e; | ||
} catch (Exception e) { | ||
OLogManager.instance().debug(this, "Error on connecting to %s", e, iServerURL); | ||
throw new OIOException("Error on connecting to " + iServerURL, e); | ||
} | ||
} | ||
|
||
@Override | ||
public OChannelBinaryAsynchClient createNewResource(String iKey, Object... iAdditionalArgs) { | ||
return createNetworkConnection(iKey, (OContextConfiguration) iAdditionalArgs[0], (Map<String, Object>) iAdditionalArgs[1]); | ||
} | ||
|
||
@Override | ||
public boolean reuseResource(String iKey, Object[] iAdditionalArgs, OChannelBinaryAsynchClient iValue) { | ||
return iValue.isConnected(); | ||
} | ||
|
||
public OResourcePool<String, OChannelBinaryAsynchClient> getPool() { | ||
return pool; | ||
} | ||
|
||
@Override | ||
public void onChannelClose(final OChannel channel) { | ||
OChannelBinaryAsynchClient conn = (OChannelBinaryAsynchClient) channel; | ||
|
||
if (pool == null) | ||
throw new IllegalStateException("Connection cannot be released because the pool doesn't exist anymore"); | ||
|
||
pool.remove(conn); | ||
|
||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...c/main/java/com/orientechnologies/orient/client/remote/ORemoteConnectionPushListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.orientechnologies.orient.client.remote; | ||
|
||
import com.orientechnologies.orient.core.sql.query.OLiveResultListener; | ||
import com.orientechnologies.orient.enterprise.channel.binary.ORemoteServerEventListener; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* Created by tglman on 01/10/15. | ||
*/ | ||
public class ORemoteConnectionPushListener implements ORemoteServerEventListener { | ||
|
||
private Set<ORemoteServerEventListener> listeners = Collections.synchronizedSet(new HashSet<ORemoteServerEventListener>()); | ||
|
||
public void addListener(ORemoteServerEventListener listener) { | ||
this.listeners.add(listener); | ||
} | ||
|
||
public void removeListener(ORemoteServerEventListener listener) { | ||
this.listeners.remove(listener); | ||
} | ||
|
||
public void onRequest(final byte iRequestCode, Object obj) { | ||
for (ORemoteServerEventListener listener : listeners) { | ||
listener.onRequest(iRequestCode, obj); | ||
} | ||
} | ||
|
||
@Override | ||
public void registerLiveListener(Integer id, OLiveResultListener listener) { | ||
|
||
} | ||
|
||
@Override | ||
public void unregisterLiveListener(Integer id) { | ||
|
||
} | ||
|
||
|
||
} |