testStarted
methodを使用すると、すべてのスレッドの接続を初期化できます。このメソッドは、スレッドがクローンされる前に一度だけ実行されるので、testStarted
には接続プールが必要です。スレッドはそれから取得できます。たとえば、非常にプリミティブな接続プールは、キーと接続オブジェクトのいくつかのシーケンシャルIDを持つマップになります。各スレッドは、スレッドの数に基づいて、そのプールからの接続を1つ取る:
をので、このようなシンプルなプールのように初期化することができます。
@Override
public void testStarted()
{
int maxConnections = getThreadContext().getThreadGroup().getNumThreads();
ConcurrentMap<Integer, Object> connections = new ConcurrentHashMap<Integer, Object>();
for(int i = 0; i < maxConnections; i++)
{
Object connection = //... whatever you need to do to connect
connections.put(new Integer(i), connection);
}
// Put in the context of thread group
JMeterContextService.getContext().getVariables().putObject("MyConnections", connections);
}
(接続オブジェクトは、あなたのニーズに基づいて、より具体的なタイプとすることもできます) 。私は単純にスレッド実行時に返される数値と私は、接続の初期化のために使用される初期シーケンシャル整数間の完全なマッピングを想定し
ここ
// Get connections pool from context
ConcurrentMap<Integer, Object> connections = (ConcurrentHashMap<Integer, Object>) JMeterContextService.getContext().getVariables().getObject("MyConnections");
// Find connection by thread ID, so each thread goes to a different connection
connections.get(getThreadContext().getThreadNum());
:
後で、sample
方法でそれを使用することができます。最良の仮定ではなく、改善することができますが、有効な出発点です。
testEnded methodで接続を閉じて削除できます。この方法は、また、一度実行しますので、我々はすべての接続を閉じる:
@Override
public void testEnded()
{
for(Entry<Integer, Object> connection : connections.entrySet())
{
connection.close(); // or do whatever you need to close it
connections.remove(connection.getKey());
}
}
またはすべての接続が閉じているときにだけconnections.clear()
を呼び出すことができます。
開示:私はこの回答のコードを直接テストしませんでしたが、過去に同様のコードの断片を使用して、この質問に答えるために再利用しました。問題が見つかった場合は、この回答を更新してください。
@ user7294900リンクを共有していただきありがとうございます。私はこのリンクを既に訪れていますが、私の要求に合っていません。なぜなら、私は接続の共有を実装する方法を理解する必要があるカスタムサンプラーだからです。 – VinothNair