我有一個名爲qoute的java進程獨立運行在linux server1和server2上。多線程使用的java集合的複製
這qoute過程用於創建使用Spring RMI和在該線程更新「quotableSet」收集和的PriorityBlockingQueue「addSymQ」用一組符號的QuotingThread。
<bean id="quotes-core" class="com.process.quotes.QuotesHandler" scope="singleton" init-method="init">
<constructor-arg>
<bean id="continuousQuotingThread" class="com.process.quotes.QuotingThread" scope="singleton" >
<property name="futureTaskUtil" ref="futureTaskUtil" />
<property name="continueToProcess" ref="continueToProcess" />
<property name="addSymQ" ref="addSymQ" />
</bean>
</constructor-arg>
</bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="Quotes-Core" />
<property name="service" ref="quotes-core" />
<property name="serviceInterface" value="com.remote.QuotesHandlerIF" />
<property name="registryPort" value="${${quotes-processor-port}}" />
</bean>
的QuotingThread更新「quotableSet」並且基於來自被構造這樣的QuotesClient符號「addSymQ」。
<bean id="quotes" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://${${quotes-processor-host}}:${${quotes-processor-port}}/Quotes-Core" />
<property name="serviceInterface" value="com.remote.QuotesHandlerIF" />
<property name="refreshStubOnConnectFailure" value="true" />
<property name="lookupStubOnStartup" value="false" />
</bean>
作爲2名獨立的QuotingThread的是通過使用彈簧則Rmi每個報價過程創建的,
例如,符號由報價過程1和符號E中,B,C被添加到 'quotableSet', 'addSymQ' ,f,g通過引用process2添加到'quotableSet','addSymQ'。
並且添加這樣做。
QuotingThread:
--------------------------------
public void addSymbols(String commaDelimSymbolsList) {
if (null != commaDelimSymbolsList && commaDelimSymbolsList.length() > 0) {
String[] symAr = commaDelimSymbolsList.split(",");
for (int i = 0; i < symAr.length; i++) {
addSymQ.add(sec.getUniqueSymbol());
}
}
}
public void run() {
while (continueToProcess.get()) {
try {
while (addSymQ.peek() != null) {
String symbol = addSymQ.poll();
quotableSet.add(symbol);
// some usage with quotable set
}
Iterator<String> ite = quotableSet.iterator();
while (ite.hasNext()) {
String symbol = ite.next();
if (symbol != null && symbol.trim().length() > 0) {
// some usage with quotable set
}
}
Thread.sleep(2000);
} catch (Exception e) {
logger.error("Exception : ", e);
}
}
}
'quotableSet' 和 'addSymQ' 聲明這樣,
private Set<String> quotableSet = Collections.synchronizedSet(new HashSet<String>(Arrays.asList("DJ!DJI,NQ!COMP,CX!SPX,RU!RUT,CX!VIX,CX!TNX");
private PriorityBlockingQueue<String> addSymQ;
我需要的是什麼符號應在兩個線程的 'quotableSet' 和 'addSymQ' 集合被複制。
我的意思是符號a,b,c,e,f,g應該同時添加到線程'quotableSet'和'addSymQ'集合中。
所以,如果一個服務器出現故障,服務器可以應用第二個服務器..
誰能幫助我在這個問題上?
是的,我需要的是在兩個不同的JVM上運行的兩個報價進程間的同步......但是可以使用spring RMI而不是套接字編程嗎?因爲我已經在我的應用程序中實現了spring ... – Sandy
是的,RMI也可以工作。 –