Skip to main content

@PostConstruct with Unckecked Expcetion

3 replies [Last post]
dernasherbrezon
Offline
Joined: 2007-06-17
Points: 0

I've got simple MDB component with @PostConstruct.

@MessageDriven(mappedName = "incoming")
public class SMSSender implements MessageListener
{
private Connection connection;
private Destination outgoing;

@PostConstruct
public void onConstruct()
{
InitialContext ctx;
try {
ctx = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("outgoing_factory");
connection = connectionFactory.createConnection();
outgoing = (Destination) ctx.lookup("outgoing");
} catch (Exception e) {
throw new RuntimeException("Cannot find needed jms resources",e);
}

}

@PreDestroy
public void onDestroy()
{
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
log.error("Cannot close jms connection", e);
}
}
}

@SuppressWarnings("unchecked")
public void onMessage(Message message)
{
// save in DB for example
// and send in jms queue "outgoing"
}
}

But sometimes i've got exception. "Cannot allocate more jms connections......etc"
Is there are some way to pool jms connections for "outgoing" or how i must configure outgoing_factory in case of growing mdbs (more applications will be deployed)?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mvatkina
Offline
Joined: 2005-04-04
Points: 0

Do you need as many connections as there are bean instances? You are creating it on an instance construct, and closing it only when and if the instance is destroyed.

Regards,
-marina

dernasherbrezon
Offline
Joined: 2007-06-17
Points: 0

Yes i want. To improve perfomance. Is there any other trick to do so? Or any some property to configure mdb instances per mdb definition?

PS: Марина? о_О

ksak
Offline
Joined: 2005-05-20
Points: 0

It's preferable to use a resource-ref for your MDB connection factory : e.g.

@Resource(mappedName="outgoing_factory") QueueConnectionFactory outgoing;

Then acquire a connection only when you want to send a message and close it. The
underlying connection will be pooled so you shouldn't experience significant overhead
in acquiring the connection each time.