nithril / smtp-connection-pool

SMTP Connection Pool
Apache License 2.0
47 stars 19 forks source link

How to reconnect the transport object. #13

Closed rohandodeja closed 7 years ago

rohandodeja commented 7 years ago

I used

transport.isConnected() which results false sometime .

now I'm using connect method as follows :-

((SMTPTransport) transport.getDelegate()).connect();

now how do i set this connected connection to pool again as i got disconnected transport object .

nithril commented 7 years ago

Could you post a full example of how you send an email, including factory configuration and connection borrows?

rohandodeja commented 7 years ago

Factory Config:-

Properties props = conn.getSmtpPropeprties(); Authenticator auth=null; props.put("mail.smtp.auth", "true"); //enable authentication auth = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(props.getProperty("user"), props.getProperty("password")); } }; props.setProperty("mail.smtp.timeout", GatewayConstants.SMTP_TIMEOUT); props.setProperty("mail.smtp.connectiontimeout",GatewayConstants.SMTP_CONNECTION_TIMEOUT); props.setProperty("mail.smtp.writetimeout",GatewayConstants.SMTP_WRITE_TIMEOUT); Session session = Session.getInstance(props, auth); SmtpConnectionFactory factory = SmtpConnectionFactories.newSmtpFactory(session); GenericObjectPoolConfig config = new GenericObjectPoolConfig();

SmtpConnectionPool smtpConnectionPool = new SmtpConnectionPool(factory, config);

Sending email :-

try(ClosableSmtpConnection transport = SmtpConnectionPools.getPoolConnection(getConnectionId())){

if(!transport.isConnected()){ //got disconnected transport object reconnecting it. logger.info("got disconnected transport object reconnecting it."); ((SMTPTransport) transport.getDelegate()).connect(); } transport.sendMessage(mmm, mmm.getAllRecipients()); String response=((SMTPTransport) transport.getDelegate()).getLastServerResponse();

nithril commented 7 years ago

What is SmtpConnectionPools and what does this method do SmtpConnectionPools.getPoolConnection(getConnectionId()) ?

rohandodeja commented 7 years ago

Actually this method internally fetch a connection from pool :-

transport = fetchedPool.borrowObject();

nithril commented 7 years ago

Try to activate the test on borrow, eg.:

        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setTestOnBorrow(true);
rohandodeja commented 7 years ago

Ok , so this method will ensure that pooled connection will be returned in connected state for use, and also when connection is returned back to pool after use then also it will be saved in connected state?

nithril commented 7 years ago

This method only ensure that pooled transport will be returned in connected state for use.

If the transport is not connected, the primer is destroyed and a new connection is created.

rohandodeja commented 7 years ago

Appreciate for help, no issues for now :+1: .

nithril commented 7 years ago

Great!

I have added a PoolConfigs to ease pool configuration