use of jp.ossc.nimbus.service.publish.Message in project nimbus by nimbus-org.
the class DistributedSharedContextService method onRehashRequest.
protected Message onRehashRequest(final DistributedSharedContextEvent event, final Object sourceId, final int sequence, final String responseSubject, final String responseKey) {
if (isMain()) {
Thread rehashThread = new Thread() {
public void run() {
Message response = null;
try {
rehash(((Long) event.value).longValue());
response = createResponseMessage(responseSubject, responseKey, null);
} catch (Throwable th) {
response = createResponseMessage(responseSubject, responseKey, th);
}
try {
serverConnection.response(sourceId, sequence, response);
} catch (MessageSendException e) {
getLogger().write("DSCS_00002", new Object[] { isClient ? clientSubject : subject, response }, e);
}
}
};
rehashThread.start();
}
return null;
}
use of jp.ossc.nimbus.service.publish.Message in project nimbus by nimbus-org.
the class DistributedSharedContextService method getClientMemberIdSet.
public Set getClientMemberIdSet() {
if (serverConnection == null || clientSubject == null) {
return new HashSet();
}
try {
Message message = serverConnection.createMessage(clientSubject, null);
Set result = serverConnection.getReceiveClientIds(message);
if (isClient) {
result.add(getId());
}
return result;
} catch (MessageException e) {
return new HashSet();
}
}
use of jp.ossc.nimbus.service.publish.Message in project nimbus by nimbus-org.
the class SharedQueueService method lockFirst.
protected Object lockFirst(long timeout) throws SharedContextSendException, SharedContextTimeoutException {
if (isMain()) {
List keys = null;
if (context.size() != 0) {
synchronized (context) {
if (context.size() != 0) {
Iterator itr = context.keySet().iterator();
for (int i = 0; i < seekDepth && itr.hasNext(); i++) {
if (keys == null) {
keys = new ArrayList();
}
keys.add(itr.next());
}
}
}
}
if (keys == null || keys.size() == 0) {
return null;
}
for (int i = 0; i < keys.size(); i++) {
Object key = keys.get(i);
try {
if (lock(key, true, true, timeout)) {
return key;
}
} catch (SharedContextTimeoutException e) {
continue;
}
}
return null;
} else {
Object lockedKey = null;
final long start = System.currentTimeMillis();
try {
String key = getId().toString() + Thread.currentThread().getId();
Message message = serverConnection.createMessage(subject, key);
Set receiveClients = serverConnection.getReceiveClientIds(message);
if (receiveClients.size() != 0) {
message.setObject(new SharedQueueEvent(SharedQueueEvent.EVENT_LOCK_FIRST, null, new Object[] { new Long(Thread.currentThread().getId()), new Long(timeout) }));
Message[] responses = serverConnection.request(message, isClient ? clientSubject : subject, key, 1, timeout);
Object ret = responses[0].getObject();
responses[0].recycle();
if (ret instanceof Throwable) {
throw new SharedContextSendException((Throwable) ret);
} else {
lockedKey = ret;
}
} else {
throw new NoConnectServerException("Main server is not found.");
}
} catch (MessageException e) {
throw new SharedContextSendException(e);
} catch (MessageSendException e) {
throw new SharedContextSendException(e);
} catch (RequestTimeoutException e) {
final boolean isNoTimeout = timeout <= 0;
timeout = isNoTimeout ? timeout : timeout - (System.currentTimeMillis() - start);
if (!isNoTimeout && timeout <= 0) {
throw new SharedContextTimeoutException("timeout=" + timeout + ", processTime=" + (System.currentTimeMillis() - start), e);
} else {
return lockFirst(timeout);
}
} catch (RuntimeException e) {
throw e;
} catch (Error e) {
throw e;
}
if (lockedKey != null) {
Lock lock = null;
synchronized (keyLockMap) {
lock = (Lock) keyLockMap.get(lockedKey);
if (lock == null) {
lock = new Lock(lockedKey);
keyLockMap.put(lockedKey, lock);
}
}
final boolean isNoTimeout = timeout <= 0;
timeout = isNoTimeout ? timeout : timeout - (System.currentTimeMillis() - start);
if (!isNoTimeout && timeout <= 0) {
unlock(lockedKey);
throw new SharedContextTimeoutException("timeout=" + timeout + ", processTime=" + (System.currentTimeMillis() - start));
} else if (!lock.acquire(getId(), true, timeout)) {
unlock(lockedKey);
return null;
}
}
return lockedKey;
}
}
use of jp.ossc.nimbus.service.publish.Message in project nimbus by nimbus-org.
the class SharedQueueService method onPut.
protected Message onPut(final SharedContextEvent event, final Object sourceId, final int sequence, final String responseSubject, final String responseKey) {
Message ret = super.onPut(event, sourceId, sequence, responseSubject, responseKey);
pushAfter();
return ret;
}
use of jp.ossc.nimbus.service.publish.Message in project nimbus by nimbus-org.
the class ServerConnectionSendActionService method execute.
/**
* リソースの内容を読み込んで、{@link ServerConnection}に{@link Message}を送信する。<p>
* リソースのフォーマットは、以下。<br>
* <pre>
* subject,key
*
* objectId
* objectScript
* </pre>
* subjectは、{@link Message}に設定するサブジェクトを指定する。keyは、{@link Message}に設定するキーを指定する。サブジェクトを複数設定する場合は、改行して指定する。サブジェクト指定の終了には、空行を挿入する。<br>
* objectIdは、{@link Message}に設定するオブジェクトを指定するもので、同一テストケース中に、このTestActionより前に、オブジェクトを戻すテストアクションが存在する場合は、そのアクションIDを指定する。また、同一シナリオ中に、このTestActionより前に、オブジェクトを戻すテストアクションが存在する場合は、テストケースIDとアクションIDをカンマ区切りで指定する。空行を指定した場合は、オブジェクトをTestActionの結果から取得しない。<br>
* objectScriptは、{@link Message}に設定するオブジェクトを生成するスクリプトを指定する。スクリプトは、{@link Interpreter#evaluate(String,Map)}で評価され、引数の変数マップには、"context"で{@link TestContext}、"preResult"でpreResultが渡される。<br>
* objectId、objectScriptの両方が指定されていない場合は、preResultを使用する。<br>
*
* @param context コンテキスト
* @param actionId アクションID
* @param preResult 更新クエリに対する引数オブジェクト
* @param resource リソース
* @return 送信したメッセージ
*/
public Object execute(TestContext context, String actionId, Object preResult, Reader resource) throws Exception {
BufferedReader br = new BufferedReader(resource);
Message message = null;
Object object = preResult;
ServerConnectionFactory scf = serverConnectionFactory;
if (serverConnectionFactory == null && serverConnectionFactoryServiceName != null) {
scf = (ServerConnectionFactory) ServiceManagerFactory.getServiceObject(serverConnectionFactoryServiceName);
}
final ServerConnection con = scf.getServerConnection();
try {
String subjectAndKey = br.readLine();
if (subjectAndKey == null || subjectAndKey.length() == 0) {
throw new Exception("Unexpected EOF on subject and key");
}
do {
String[] subjectAndKeyArray = CSVReader.toArray(subjectAndKey, ',', '\\', null, null, true, false, true, true);
if (subjectAndKeyArray == null || subjectAndKeyArray.length == 0 || subjectAndKeyArray.length > 2) {
throw new Exception("Illegal subject and key format. subjectAndKey=" + subjectAndKey);
}
if (message == null) {
message = con.createMessage(subjectAndKeyArray[0], subjectAndKeyArray.length == 2 ? subjectAndKeyArray[1] : null);
} else {
message.setSubject(subjectAndKeyArray[0], subjectAndKeyArray.length == 2 ? subjectAndKeyArray[1] : null);
}
} while ((subjectAndKey = br.readLine()) != null && subjectAndKey.length() != 0);
final String objectId = br.readLine();
if (objectId != null && objectId.length() != 0) {
Object actionResult = null;
if (objectId.indexOf(",") == -1) {
actionResult = context.getTestActionResult(objectId);
} else {
String[] ids = objectId.split(",");
if (ids.length != 2) {
throw new Exception("Illegal objectId format. id=" + objectId);
}
actionResult = context.getTestActionResult(ids[0], ids[1]);
}
if (actionResult == null) {
throw new Exception("TestActionResult not found. id=" + objectId);
}
object = actionResult;
}
String objectScript = null;
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
try {
String line = null;
while ((line = br.readLine()) != null) {
pw.println(line);
}
pw.flush();
objectScript = sw.toString();
if (objectScript.length() == 0) {
objectScript = null;
}
} finally {
sw.close();
pw.close();
}
if (objectScript != null) {
if (interpreter == null) {
throw new UnsupportedOperationException("Interpreter is null.");
}
final Map params = new HashMap();
params.put("context", context);
params.put("preResult", preResult);
object = interpreter.evaluate(objectScript, params);
}
if (object != null) {
message.setObject(object);
}
con.send(message);
} finally {
br.close();
br = null;
}
return message;
}
Aggregations