Ok I have process your posts over and over here and I just need your blessing.
I have rid of the Singletons and now I am creating dynamiclly/programaticlly prototype beans using application context this way:
Code:
FixSessionDTO fixSessionDTO = new FixSessionDTO();
fixSessionDTO.setInetrnalSessionId(internalSessionId);
SessionMDB sessionMDB = (SessionMDB) context.getBean("sessionMDB");
sessionMDB.setFixSessionDTO(fixSessionDTO);
sessionMDB.setTopicDestination(topicDestination);
sessionMDB.init();
now SessionMdb looks like this:
@Scope(value="prototype")
@Component("sessionMDB")
public class SessionMDB extends AbstractSessionBean
{
private SubscribableChannel channel;
public SessionMDB(FixSessionDTO fixSessionDTO, Topic topicDestination)
{
super(fixSessionDTO, topicDestination);
}
public SessionMDB()
{
super();
// TODO Auto-generated constructor stub
}
private final static Logger log = Logger.getLogger(SessionMDB.class);
@Override
public void onMessage(Message message)
{
}
}
Code:
@ManagedResource
public abstract class AbstractSessionBean implements MessageListener//, AbstractSessionBeanManagedOperations//
{
protected FixSessionDTO fixSessionDTO;
protected static Logger log = Logger.getLogger(AbstractSessionBean.class);;
@Autowired
ConnectionFactory connectionFactory;
DefaultMessageListenerContainer messageListeners;
private Topic topicDestination;
public Topic getTopicDestination()
{
return topicDestination;
}
public AbstractSessionBean()
{
super();
// TODO Auto-generated constructor stub
}
public void setTopicDestination(Topic topicDestination)
{
this.topicDestination = topicDestination;
}
public AbstractSessionBean(FixSessionDTO fixSessionDTO, Topic topicDestination)
{
super();
this.fixSessionDTO = fixSessionDTO;
this.topicDestination = topicDestination;
}
public FixSessionDTO getFixSessionDTO()
{
return fixSessionDTO;
}
public void setFixSessionDTO(FixSessionDTO fixSessionDTO)
{
this.fixSessionDTO = fixSessionDTO;
}
//@PostConstruct
public void init()
{
try
{
System.out.println("AbstractSessionBean.init()." + fixSessionDTO.toString());
messageListeners = new DefaultMessageListenerContainer();
CachingConnectionFactory cacheFactory = new CachingConnectionFactory();
cacheFactory.setTargetConnectionFactory(connectionFactory);
cacheFactory.setReconnectOnException(true);
cacheFactory.setExceptionListener(new ExceptionListener()
{
public void onException(JMSException e)
{
log.error("Error", e);
}
});
cacheFactory.setSessionCacheSize(1);
messageListeners.setConnectionFactory(connectionFactory);
messageListeners.setDestinationName(topicDestination.getTopicName());
messageListeners.setMaxConcurrentConsumers(1);
messageListeners.setSessionAcknowledgeModeName("AUTO_ACKNOWLEDGE");
messageListeners.setMessageListener(this);
messageListeners.setPubSubDomain(true);
messageListeners.setAcceptMessagesWhileStopping(false);
messageListeners.setConcurrentConsumers(1);
messageListeners.setSessionTransacted(false);
messageListeners.setReceiveTimeout(1000);
messageListeners.setMaxMessagesPerTask(300);
messageListeners.initialize();
messageListeners.start();
}
catch (Exception e)
{
e.printStackTrace();
}
}
@PreDestroy
public void destroy()
{
try
{
log.debug("Destroying MDB. details=" + fixSessionDTO.toString());
if (messageListeners != null)
{
messageListeners.stop();
messageListeners.shutdown();
messageListeners.destroy();
}
}
catch (Exception e)
{
log.error("Couldnt destroy MDB. details=" + fixSessionDTO.toString(), e);
}
}
@ManagedOperation
public void stop()
{
messageListeners.stop();
}
@ManagedOperation
public void start()
{
messageListeners.start();
}
two questions:
1. I thought if I need to use Spring integration in order to have it's Messages mechanisem why couldnt I use the same topic I am already subscribed to retrieve "adminisitartion" messages to do administration things like 'stop','start','die'..
2. incase you like idea 1. how will I destroy the prototype bean. just called this.destroy() and close all connections and hope the GC will do the rest for me?