コードを本番環境に移行する前に確認したい。
以下のように LinkedBlockingQueue を使用しています。1 つのプロデューサー スレッドと 5 つのコンシューマー スレッドがあります。コンシューマー スレッドは DB 操作を行うため、プロセスを高速化するためにスレッドを 5 に増やしました。
さて、コードまたはDBレベルで(同期/行の複数の更新/または注意が必要な問題)問題に遭遇する方法はありますか。
以下は私の完全なコードです。
private int numberOfConsumers = 5;
private LinkedBlockingQueue<RequestParameters> processorQueue;
public void init(){
processorQueue = new LinkedBlockingQueue<RequestParameters>();
for (int i = 0; i < numberOfConsumers ; i++) {
try {
QueueConsumer c = new QueueConsumer(processorQueue);
c.setName("ThreadName-"+i);
c.start();
} catch (Exception e) {
logger.error("", e);
}
}
this.postCallProcessorDaemon = this;
}
class QueueConsumer extends Thread {
private Logger log = Logger.getLogger(QueueConsumer.class);
private final BlockingQueue<RequestParameters> queue;
QueueConsumer(BlockingQueue<RequestParameters> q) { queue = q; }
public void run() {
try {
while (true) {
RequestParameters rp = queue.take();
consumeRecord(rp);
}
} catch (Exception ex) {
log.error("Exception in run method", ex);
}
}
void consumeRecord(RequestParameters requestParameters)
{
try{
process(requestParameters);
} catch (Throwable e) {
log.error("Exception",e);
}
}
private void process(RequestParameters requestParameters) throws Exception{
for (Action each : allActions) {
if(each.doProcessing(requestParameters)){
boolean status = each.process(requestParameters);
}
}
}
}
public boolean process(RequestParameters parameters) {
//In my process method, I am inserting rows in table based on data in Queue.
}
ORM マッピングに使用している HBM ファイル
<class name="CampaignSubscriptionsSummary" table="campaign_subscriptions_summary">
<composite-id name="id" class="com.on.robd.config.db.reports.CampaignSubscriptionsSummaryId">
<key-property name="reportTime" type="timestamp">
<column name="REPORT_TIME" length="19" />
</key-property>
<key-property name="campaignId" type="long">
<column name="CAMPAIGN_ID" />
</key-property>
<key-property name="recipient" type="string">
<column name="RECIPIENT" length="24" />
</key-property>
<key-property name="selectedPack" type="string">
<column name="SELECTED_PACK" length="256" />
</key-property>
</composite-id>
<many-to-one name="campaign" class="com.on.robd.config.db.campaigns.Campaign" update="false" insert="false" fetch="select">
<column name="CAMPAIGN_ID" not-null="true" />
</many-to-one>
<many-to-one name="campaignSubscriptionsStatusDim" class="com.on.robd.config.db.reports.CampaignSubscriptionStatusDim" fetch="select">
<column name="SUBSCRIPTION_STATUS_ID" not-null="true" />
</many-to-one>
<property name="sender" type="string">
<column name="SENDER" length="24" not-null="true" />
</property>
<property name="callDuration" type="java.lang.Integer">
<column name="CALL_DURATION" />
</property>
<property name="dtmfInput" type="string">
<column name="DTMF_INPUT" length="16" not-null="true" />
</property>
<property name="promptForPack" type="string">
<column name="PROMPT_FOR_PACK" length="256" />
</property>
<property name="wavFile" type="string">
<column name="WAV_FILE" length="256" />
</property>
<property name="subscriptionUrl" type="string">
<column name="SUBSCRIPTION_URL" length="256" />
</property>
<property name="language" type="string">
<column name="LANGUAGE" length="256" />
</property>
<property name="reobdTime" type="timestamp">
<column name="REOBD_TIME" length="19" />
</property>
<many-to-one name="campaignTypeDim" class="com.on.robd.config.db.reports.CampaignTypeDim" fetch="select">
<column name="CAMPAIGN_TYPE_ID" not-null="true" />
</many-to-one>
</class>