如何编写线程安全的 Spring Web 应用程序?
当我们一次发送 250 个请求时,就会发生死锁。如何解决这个问题?
1.Controller - 这个控制器接收来自调用者的请求。当所有 250 个线程同时命中时。
public class TestController extends MultiActionController {
private SyncService synchronizationService;
public void setSynchronizationService(SyncService synchronizationService) {
this.synchronizationService = synchronizationService;
}
public void StoreData(HttpServletRequest request, HttpServletResponse response) {
byte[] syncData = null;
byte[] returnValue = null;
try {
syncData = readData(request);
returnValue = synchronizationService.receiveData(syncData);
} catch (Exception e) {
}
}
2.Helper - 这会解析输入并填充表格。
public class TestImpl implements SyncService {
private DataParser dataParser = null;
private QueueHelper queueHelper = null;
public void setDataParser(DataParser dataParser) {
this.dataParser = dataParser;
}
public void setQueueHelper(QueueHelper queueHelper) {
this.queueHelper = queueHelper;
}
public byte[] receiveData(byte[] data) throws HibernateException, NumberFormatException {
boolean addTransInQueue = false;
String dataString = null;
HashMap syncHashMap = null;
try {
InSyncModel syncModel = dataParser.parseData(dataString);
addTransInQueue = queueHelper.addTransInQueue(syncModel, data);
return "Succ".getBytes();
} catch (JsonSyntaxException jse) {
throw jse;
}
}
}
3.Configurations - 用于上述代码的 bean 注入。
<bean name="synchronizationController" scope="request" class="TestController">
<property name="synchronizationService" ref="synchronizationService"/>
</bean>
<bean name="synchronizationService" scope="request" class="TestImpl">
<property name="dataParser" ref="dataParserObj"></property>
<property name="queueHelper" ref="queueHelperObj"></property>
</bean>
<bean name="dataParserObj" scope="request" class="DataParser"/>
<bean name="queueHelperObj" scope="request" class="QueueHelper">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>