I am having a java process called qoute running on linux server1 and server2 independantly.
This qoute process is used to creates a QuotingThread using Spring Rmi and in that thread update ‘quotableSet’ collection and PriorityBlockingQueue ‘addSymQ’ with a set of symbols.
<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>
The QuotingThread updates the ‘quotableSet’ and ‘addSymQ’ based on the symbols from the QuotesClient which is configured this way.
<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>
As 2 seperate QuotingThread’s are created by each quote process using Spring Rmi ,
For example, symbols a,b,c are added to ‘quotableSet’,’addSymQ’ by quote process1 and symbols e,f,g are added to ‘quotableSet’,’addSymQ’ by quote process2.
And addition is done this way.
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’ and ‘addSymQ’ is declared this way,
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;
What i need is symbols should be replicated on both thread’s ‘quotableSet’ and ‘addSymQ’ collections .
I mean symbols a,b,c,e,f,g should be added to both threads ‘quotableSet’ and ‘addSymQ’ collections equally.
So, that if one server goes down, second server can server the application..
can anyone help me on this issue?
Let me see if I understand this correctly.
You have two servers, each one running a single instance of the quote process. There are 2 threads in the quote process that service these collections. Now where I get confused is you say “should be replicated on both thread’s ‘quotableSet’ and ‘addSymQ’ collections” does that mean you want to have the collections shared between the 2 threads in the quote process? If so that is WAY easier than what I get from this – “if one server goes down, second server can server the application” which indicates that you want to have the collections synchronized between the two servers, not the 2 threads.
Or are you asking for both threads and processes to be synchronized? Or can the two threads have difference values in the collections as long as the collections themselves are replicated across the servers?
Here are some hints on each:
1) Synchronized across threads. Simply make the collections available in a shared resource across the two threads, ensure you have synchronized the collections. And there you go.
2) Synchronized across processes. You will need to have some connection like a socket between your two processes that allow them to send and receive each others collections values. This will require an additional thread on in the quote process to handle this.
Good Luck.