下面列出了javax.jms.JMSException#setLinkedException ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates or passes through a JMSException to be thrown to the client.
*
* In the event that the exception passed to this method is already a
* JMSException it is passed through unmodified, otherwise a new JMSException
* is created with the given message and the cause is set to the given
* cause Throwable instance.
*
* @param message
* The message value to set when a new JMSException is created.
* @param cause
* The exception that caused this error state.
*
* @return a JMSException instance.
*/
public static JMSException create(String message, Throwable cause) {
if (cause instanceof JMSException) {
return (JMSException) cause;
}
if (cause.getCause() instanceof JMSException) {
return (JMSException) cause.getCause();
}
if (message == null || message.isEmpty()) {
message = cause.getMessage();
if (message == null || message.isEmpty()) {
message = cause.toString();
}
}
JMSException exception = new JMSException(message);
if (cause instanceof Exception) {
exception.setLinkedException((Exception) cause);
}
exception.initCause(cause);
return exception;
}
@Override
public void setClientID(final String clientID) throws JMSException {
checkClosed();
if (this.clientID != null) {
throw new IllegalStateException("Client id has already been set");
}
if (!justCreated) {
throw new IllegalStateException("setClientID can only be called directly after the connection is created");
}
try {
validateClientID(initialSession, clientID);
this.clientID = clientID;
this.addSessionMetaData(initialSession);
} catch (ActiveMQException e) {
JMSException ex = new JMSException("Internal error setting metadata jms-client-id");
ex.setLinkedException(e);
ex.initCause(e);
throw ex;
}
justCreated = false;
}
@Override
public void setObject(final Serializable object) throws JMSException {
checkWrite();
if (object != null) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.flush();
data = baos.toByteArray();
} catch (Exception e) {
JMSException je = new JMSException("Failed to serialize object");
je.setLinkedException(e);
je.initCause(e);
throw je;
}
}
}
/**
* Creates or passes through a JMSException to be thrown to the client.
*
* In the event that the exception passed to this method is already a
* JMSException it is passed through unmodified, otherwise a new JMSException
* is created with the given message and the cause is set to the given
* cause Throwable instance.
*
* @param message
* The message value to set when a new JMSException is created.
* @param cause
* The exception that caused this error state.
*
* @return a JMSException instance.
*/
public static JMSException create(String message, Throwable cause) {
if (cause instanceof JMSException) {
return (JMSException) cause;
}
if (cause.getCause() instanceof JMSException) {
return (JMSException) cause.getCause();
} else if (cause instanceof ProviderException) {
return ((ProviderException) cause).toJMSException();
}
if (message == null || message.isEmpty()) {
message = cause.getMessage();
if (message == null || message.isEmpty()) {
message = cause.toString();
}
}
JMSException exception = new JMSException(message);
if (cause instanceof Exception) {
exception.setLinkedException((Exception) cause);
}
exception.initCause(cause);
return exception;
}
@Test
public void testExceptionStackTrace() {
JMSException jmsEx = new JMSException("could not connect");
Exception innerEx = new Exception("host not found");
jmsEx.setLinkedException(innerEx);
JmsException springJmsEx = JmsUtils.convertJmsAccessException(jmsEx);
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
springJmsEx.printStackTrace(out);
String trace = sw.toString();
assertTrue("inner jms exception not found", trace.indexOf("host not found") > 0);
}
@Test
public void testExceptionStackTrace() {
JMSException jmsEx = new JMSException("could not connect");
Exception innerEx = new Exception("host not found");
jmsEx.setLinkedException(innerEx);
JmsException springJmsEx = JmsUtils.convertJmsAccessException(jmsEx);
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
springJmsEx.printStackTrace(out);
String trace = sw.toString();
assertTrue("inner jms exception not found", trace.indexOf("host not found") > 0);
}
@Test
public void testExceptionStackTrace() {
JMSException jmsEx = new JMSException("could not connect");
Exception innerEx = new Exception("host not found");
jmsEx.setLinkedException(innerEx);
JmsException springJmsEx = JmsUtils.convertJmsAccessException(jmsEx);
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
springJmsEx.printStackTrace(out);
String trace = sw.toString();
assertTrue("inner jms exception not found", trace.indexOf("host not found") > 0);
}
@Override
public void writeUTF(final String value) throws JMSException {
checkWrite();
try {
bytesWriteUTF(message.getBodyBuffer(), value);
} catch (Exception e) {
JMSException je = new JMSException("Failed to write UTF");
je.setLinkedException(e);
je.initCause(e);
throw je;
}
}
public Message receive(long timeout) throws JMSException {
try {
Thread.sleep(timeout);
} catch (InterruptedException ex) {
logger.warn(ex.getMessage(), ex);
JMSException jmsException = new JMSException(ex.getMessage());
jmsException.setLinkedException(ex);
throw jmsException;
}
return getMessage();
}
public JMSException toJMSException() {
final JMSException jmsEx = new JMSException(getMessage());
jmsEx.initCause(this);
jmsEx.setLinkedException(this);
return jmsEx;
}