/**
* set client id
* @param conn jms connection
* @param timeout timeout
* @param unit time unit
* @return Boolean true or false
* @throws LogCollectionException LogCollectionException
* @throws IOException IOException
*/
public static Boolean setClientId(final QueueConnection conn, long timeout,
TimeUnit unit) throws LogCollectionException, IOException
{
final BlockingQueue<Object> mailbox = new ArrayBlockingQueue<Object>(1);
ExecutorService executor = Executors.newSingleThreadExecutor(new DaemonThreadFactory());
executor.submit(new Runnable()
{
public void run()
{
try
{
conn.setClientID(UUID.randomUUID().toString());
mailbox.offer(Boolean.TRUE);
}
catch (JMSException e)
{
mailbox.offer(e);
}
}
});
Object result;
try
{
result = mailbox.poll(timeout, unit);
if (result == null)
{
if (!mailbox.offer("time out"))
{
result = mailbox.take();
}
}
}
catch (InterruptedException e)
{
closeConn(conn);
throw initCause(new InterruptedIOException(e.getMessage()), e);
}
finally
{
executor.shutdownNow();
}
if (result == null)
{
closeConn(conn);
throw new SocketTimeoutException("Connect timed out");
}
if (result instanceof Boolean)
{
return (Boolean)result;
}
if (result instanceof JMSException)
{
closeConn(conn);
throw new LogCollectionException("IO Execption",
(JMSException)result);
}
throw new IOException("Unknown reason!");
}
/**
* wrapper cause.
* @param <T> throwable
* @param wrapper cause wrapper
* @param wrapped throwable
* @return
*/
private static <T extends Throwable> T initCause(T wrapper,
Throwable wrapped)
{
wrapper.initCause(wrapped);
return wrapper;
}
/**
* close connection
*
* @param conn jms connection
*/
private static void closeConn(Connection conn)
{
if (conn != null)
{
try
{
conn.close();
}
catch (JMSException e)
{
LOGGER.error(e.toString(), e);
}
}
}
}
/**
* 内部私有类
* 线程工厂,ExecutorService调用newThread方法自动创建线程。
* @version [版本号,YYYY-MM-DD]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
class DaemonThreadFactory implements ThreadFactory
{
public Thread newThread(Runnable r)
{
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
}
}
* set client id
* @param conn jms connection
* @param timeout timeout
* @param unit time unit
* @return Boolean true or false
* @throws LogCollectionException LogCollectionException
* @throws IOException IOException
*/
public static Boolean setClientId(final QueueConnection conn, long timeout,
TimeUnit unit) throws LogCollectionException, IOException
{
final BlockingQueue<Object> mailbox = new ArrayBlockingQueue<Object>(1);
ExecutorService executor = Executors.newSingleThreadExecutor(new DaemonThreadFactory());
executor.submit(new Runnable()
{
public void run()
{
try
{
conn.setClientID(UUID.randomUUID().toString());
mailbox.offer(Boolean.TRUE);
}
catch (JMSException e)
{
mailbox.offer(e);
}
}
});
Object result;
try
{
result = mailbox.poll(timeout, unit);
if (result == null)
{
if (!mailbox.offer("time out"))
{
result = mailbox.take();
}
}
}
catch (InterruptedException e)
{
closeConn(conn);
throw initCause(new InterruptedIOException(e.getMessage()), e);
}
finally
{
executor.shutdownNow();
}
if (result == null)
{
closeConn(conn);
throw new SocketTimeoutException("Connect timed out");
}
if (result instanceof Boolean)
{
return (Boolean)result;
}
if (result instanceof JMSException)
{
closeConn(conn);
throw new LogCollectionException("IO Execption",
(JMSException)result);
}
throw new IOException("Unknown reason!");
}
/**
* wrapper cause.
* @param <T> throwable
* @param wrapper cause wrapper
* @param wrapped throwable
* @return
*/
private static <T extends Throwable> T initCause(T wrapper,
Throwable wrapped)
{
wrapper.initCause(wrapped);
return wrapper;
}
/**
* close connection
*
* @param conn jms connection
*/
private static void closeConn(Connection conn)
{
if (conn != null)
{
try
{
conn.close();
}
catch (JMSException e)
{
LOGGER.error(e.toString(), e);
}
}
}
}
/**
* 内部私有类
* 线程工厂,ExecutorService调用newThread方法自动创建线程。
* @version [版本号,YYYY-MM-DD]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
class DaemonThreadFactory implements ThreadFactory
{
public Thread newThread(Runnable r)
{
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
}
}