Thank you very much for your reply!
I think that we are finally digging into the issue and I am very happy of this and I really appreciate your help.
I do understand that there is a need for you to have the full picture and I will try to explain as good as I can, I will try to make it less abstract.
There is basically a producer class that is connecting to a database to pull some data (a report) based on a particular parameter ( a string representing a month).
So basically the producer is just a class that execute some queries and build some objects based on the data from the database.
Here are the classes:
Code:
@Component("dataProducer")
public class DataProducer implements ProducerInterface{
private DataSource dataSource;
@Inject
public DataEstractor(DataSource ds) {
this.dataSource = ds;
}
public List<ResultObject> getData(String month) {
....
// use the the dataSource to get the data from the DB
}
}
@Component
public class DataConsumer implements ConsumerInterface{
private List<ResultObject> data;
@Resource(name = "resultObjectListProducer")
public void setData(List<ResultObject> data) { this.data=data; }
public void consumeData() {
....
}
}
xml bean definitions
Code:
<bean id="resultObjectListProducer"
factory-bean="dataProducer"
factory-method="getData">
<constructor-arg><value>input param</value></constructor-arg>
</bean>
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://...." />
<property name="username" value="....."/>
<property name="password" value="....."/>
<property name="idleConnectionTestPeriod" value="60"/>
<property name="idleMaxAge" value="240"/>
<property name="maxConnectionsPerPartition" value="30"/>
<property name="minConnectionsPerPartition" value="10"/>
<property name="partitionCount" value="3"/>
<property name="acquireIncrement" value="5"/>
<property name="statementsCacheSize" value="100"/>
<property name="releaseHelperThreads" value="3"/>
</bean>
In my scenario the user specify the months on which to generate the reports. So the solution of the resultObjectListProducer bean (previously called nonStaticProducerDataFactory ) is not feasible because the month is statically hard coded by the <constructor-arg><value>input param</value></constructor-arg> line in the xml config.
The program must be able to dynamically identify the list of months and use these to create the data through the data producer and present it through the data consumer.
At the end of the day, what I am trying to archive is something like (for a single thread scenario):
Code:
String[] monthList;
// get the months list from user
for(String month:monthList) {
DataConsumer csvReport= (DataConsumer)ctx.getBean(DataConsumer.class);
csvReport.consumeData();
}
The power of this is that spring take care of everything. The programmer at this point is not aware of the dataProducer, the dataSource or nothing, and on top of that is that all of the elements are amazingly loosely coupled!! The DataConsumer consumer class is completely not aware of what is a data producer!
They are just tied in the spring xml config.
Unfortunately the code I have just shown you it will work if I have just a month to work on... I will just hard code it like
<constructor-arg><value>"2012-09"</value></constructor-arg>
And it will work. But if the user needs to specify a list that is every time different, it will not. What is the best way of handling this? I don't want to build a class that link the producer and the consumer... this is not dependency injection!
what do you advice?
The next question will be about concurrency and multi threading, but I think it is better to first understand this point.
As the matter of the fact (to make it simple) let's suppose that a maximum of 5 month can be specified in the list, I would like to start a thread concurrently on each month.
I hope I have been clear enough, if not please let me know, I will be happy to clarify any points.
P.S. I would like not to have any class context aware.