1 16 17 package org.springframework.jms.remoting; 18 19 import javax.jms.JMSException ; 20 import javax.jms.Message ; 21 import javax.jms.MessageFormatException ; 22 import javax.jms.MessageProducer ; 23 import javax.jms.ObjectMessage ; 24 import javax.jms.Session ; 25 26 import org.springframework.beans.factory.InitializingBean; 27 import org.springframework.jms.listener.SessionAwareMessageListener; 28 import org.springframework.jms.support.JmsUtils; 29 import org.springframework.remoting.support.RemoteInvocation; 30 import org.springframework.remoting.support.RemoteInvocationBasedExporter; 31 import org.springframework.remoting.support.RemoteInvocationResult; 32 33 43 public class JmsInvokerServiceExporter extends RemoteInvocationBasedExporter 44 implements SessionAwareMessageListener, InitializingBean { 45 46 private boolean ignoreInvalidRequests = true; 47 48 private Object proxy; 49 50 51 59 public void setIgnoreInvalidRequests(boolean ignoreInvalidRequests) { 60 this.ignoreInvalidRequests = ignoreInvalidRequests; 61 } 62 63 public void afterPropertiesSet() { 64 this.proxy = getProxyForService(); 65 } 66 67 68 public void onMessage(Message requestMessage, Session session) throws JMSException { 69 RemoteInvocation invocation = readRemoteInvocation(requestMessage); 70 if (invocation != null) { 71 RemoteInvocationResult result = invokeAndCreateResult(invocation, this.proxy); 72 writeRemoteInvocationResult(requestMessage, session, result); 73 } 74 } 75 76 81 protected RemoteInvocation readRemoteInvocation(Message requestMessage) throws JMSException { 82 if (requestMessage instanceof ObjectMessage ) { 83 ObjectMessage objectMessage = (ObjectMessage ) requestMessage; 84 Object body = objectMessage.getObject(); 85 if (body instanceof RemoteInvocation) { 86 return (RemoteInvocation) body; 87 } 88 } 89 return onInvalidRequest(requestMessage); 90 } 91 92 93 100 protected void writeRemoteInvocationResult( 101 Message requestMessage, Session session, RemoteInvocationResult result) throws JMSException { 102 103 Message response = createResponseMessage(requestMessage, session, result); 104 MessageProducer producer = session.createProducer(requestMessage.getJMSReplyTo()); 105 try { 106 producer.send(response); 107 } 108 finally { 109 JmsUtils.closeMessageProducer(producer); 110 } 111 } 112 113 123 protected Message createResponseMessage(Message requestMessage, Session session, RemoteInvocationResult result) 124 throws JMSException { 125 126 ObjectMessage response = session.createObjectMessage(result); 129 130 response.setJMSCorrelationID(requestMessage.getJMSCorrelationID()); 132 133 return response; 134 } 135 136 146 protected RemoteInvocation onInvalidRequest(Message requestMessage) throws JMSException { 147 if (this.ignoreInvalidRequests) { 148 if (logger.isWarnEnabled()) { 149 logger.warn("Invalid request message will be discarded: " + requestMessage); 150 } 151 return null; 152 } 153 else { 154 throw new MessageFormatException ("Invalid request message: " + requestMessage); 155 } 156 } 157 158 } 159 | Popular Tags |