mirror of https://github.com/apache/activemq.git
allow priority, peristence and timeouts to be specified in REST front end and use a 'type=' attribute to make it easier to provide alternative destinations
git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@396897 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8ad6fd36a1
commit
33cf032b38
|
@ -95,6 +95,9 @@ public class MessageServlet extends MessageServletSupport {
|
|||
|
||||
TextMessage message = client.getSession().createTextMessage(text);
|
||||
appendParametersToMessage(request, message);
|
||||
boolean persistent = isSendPersistent(request);
|
||||
int priority = getSendPriority(request);
|
||||
long timeToLive = getSendTimeToLive(request);
|
||||
client.send(destination, message);
|
||||
|
||||
// lets return a unique URI for reliable messaging
|
||||
|
|
|
@ -52,8 +52,11 @@ public abstract class MessageServletSupport extends HttpServlet {
|
|||
private boolean defaultTopicFlag = true;
|
||||
private Destination defaultDestination;
|
||||
private String destinationParameter = "destination";
|
||||
private String topicParameter = "topic";
|
||||
private String typeParameter = "type";
|
||||
private String bodyParameter = "body";
|
||||
private boolean defaultMessagePersistent = true;
|
||||
private int defaultMessagePriority = 5;
|
||||
private long defaultMessageTimeToLive = 0;
|
||||
|
||||
|
||||
public void init(ServletConfig servletConfig) throws ServletException {
|
||||
|
@ -144,7 +147,7 @@ public abstract class MessageServletSupport extends HttpServlet {
|
|||
for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) iter.next();
|
||||
String name = (String) entry.getKey();
|
||||
if (!destinationParameter.equals(name) && !topicParameter.equals(name) && !bodyParameter.equals(name)) {
|
||||
if (!destinationParameter.equals(name) && !typeParameter.equals(name) && !bodyParameter.equals(name)) {
|
||||
Object value = entry.getValue();
|
||||
if (value instanceof Object[]) {
|
||||
Object[] array = (Object[]) value;
|
||||
|
@ -164,7 +167,26 @@ public abstract class MessageServletSupport extends HttpServlet {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected long getSendTimeToLive(HttpServletRequest request) {
|
||||
String text = request.getParameter("JMSTimeToLive");
|
||||
if (text != null) {
|
||||
return asLong(text);
|
||||
}
|
||||
return defaultMessageTimeToLive;
|
||||
}
|
||||
|
||||
protected int getSendPriority(HttpServletRequest request) {
|
||||
String text = request.getParameter("JMSPriority");
|
||||
if (text != null) {
|
||||
return asInt(text);
|
||||
}
|
||||
return defaultMessagePriority;
|
||||
}
|
||||
|
||||
protected boolean isSendPersistent(HttpServletRequest request) {
|
||||
return defaultMessagePersistent;
|
||||
}
|
||||
|
||||
protected Destination asDestination(Object value) {
|
||||
|
@ -208,6 +230,14 @@ public abstract class MessageServletSupport extends HttpServlet {
|
|||
return null;
|
||||
}
|
||||
|
||||
protected long asLong(String name) {
|
||||
return Long.parseLong(name);
|
||||
}
|
||||
|
||||
protected int asInt(String name) {
|
||||
return Integer.parseInt(name);
|
||||
}
|
||||
|
||||
protected String asString(Object value) {
|
||||
if (value instanceof String[]) {
|
||||
return ((String[])value)[0];
|
||||
|
@ -291,16 +321,11 @@ public abstract class MessageServletSupport extends HttpServlet {
|
|||
* @return true if the current request is for a topic destination, else false if its for a queue
|
||||
*/
|
||||
protected boolean isTopic(HttpServletRequest request) {
|
||||
boolean aTopic = defaultTopicFlag;
|
||||
String aTopicText = request.getParameter(topicParameter);
|
||||
if (aTopicText != null) {
|
||||
aTopic = asBoolean(aTopicText);
|
||||
String typeText = request.getParameter(typeParameter);
|
||||
if (typeText == null) {
|
||||
return defaultTopicFlag;
|
||||
}
|
||||
return aTopic;
|
||||
}
|
||||
|
||||
protected long asLong(String name) {
|
||||
return Long.parseLong(name);
|
||||
return typeText.equalsIgnoreCase("topic");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -52,8 +52,8 @@ import org.apache.commons.logging.LogFactory;
|
|||
import edu.emory.mathcs.backport.java.util.concurrent.Semaphore;
|
||||
|
||||
/**
|
||||
* Represents a messaging client used from inside a web container
|
||||
* typically stored inside a HttpSession
|
||||
* Represents a messaging client used from inside a web container typically
|
||||
* stored inside a HttpSession
|
||||
*
|
||||
* TODO controls to prevent DOS attacks with users requesting many consumers
|
||||
* TODO configure consumers with small prefetch.
|
||||
|
@ -69,7 +69,6 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
|||
|
||||
private static transient ConnectionFactory factory;
|
||||
|
||||
|
||||
private transient Map consumers = new HashMap();
|
||||
private transient ActiveMQConnection connection;
|
||||
private transient ActiveMQSession session;
|
||||
|
@ -78,15 +77,14 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
|||
|
||||
private final Semaphore semaphore = new Semaphore(1);
|
||||
|
||||
|
||||
/**
|
||||
* @return the web client for the current HTTP session or null if there is not a web client created yet
|
||||
* @return the web client for the current HTTP session or null if there is
|
||||
* not a web client created yet
|
||||
*/
|
||||
public static WebClient getWebClient(HttpSession session) {
|
||||
return (WebClient) session.getAttribute(webClientAttribute);
|
||||
}
|
||||
|
||||
|
||||
public static void initContext(ServletContext context) {
|
||||
initConnectionFactory(context);
|
||||
}
|
||||
|
@ -98,19 +96,15 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
|||
throw new IllegalStateException("initContext(ServletContext) not called");
|
||||
}
|
||||
|
||||
|
||||
public int getDeliveryMode() {
|
||||
return deliveryMode;
|
||||
}
|
||||
|
||||
|
||||
public void setDeliveryMode(int deliveryMode) {
|
||||
this.deliveryMode = deliveryMode;
|
||||
}
|
||||
|
||||
|
||||
public synchronized void closeConsumers()
|
||||
{
|
||||
public synchronized void closeConsumers() {
|
||||
for (Iterator it = consumers.values().iterator(); it.hasNext();) {
|
||||
MessageConsumer consumer = (MessageConsumer) it.next();
|
||||
it.remove();
|
||||
|
@ -120,8 +114,7 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
|||
((MessageAvailableConsumer) consumer).setAvailableListener(null);
|
||||
consumer.close();
|
||||
}
|
||||
catch(JMSException e)
|
||||
{
|
||||
catch (JMSException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -132,7 +125,8 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
|||
closeConsumers();
|
||||
if (connection != null)
|
||||
connection.close();
|
||||
} catch (JMSException e) {
|
||||
}
|
||||
catch (JMSException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
finally {
|
||||
|
@ -145,15 +139,12 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
|||
}
|
||||
}
|
||||
|
||||
public boolean isClosed()
|
||||
{
|
||||
public boolean isClosed() {
|
||||
return consumers == null;
|
||||
}
|
||||
|
||||
public void writeExternal(ObjectOutput out) throws IOException {
|
||||
|
||||
if (consumers!=null)
|
||||
{
|
||||
if (consumers != null) {
|
||||
out.write(consumers.size());
|
||||
Iterator i = consumers.keySet().iterator();
|
||||
while (i.hasNext())
|
||||
|
@ -172,13 +163,11 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
|||
String destinationName = in.readObject().toString();
|
||||
|
||||
try {
|
||||
Destination destination = destinationName.startsWith("topic://")
|
||||
?(Destination)getSession().createTopic(destinationName)
|
||||
Destination destination = destinationName.startsWith("topic://") ? (Destination) getSession().createTopic(destinationName)
|
||||
: (Destination) getSession().createQueue(destinationName);
|
||||
consumers.put(destination, getConsumer(destination, true));
|
||||
}
|
||||
catch (JMSException e)
|
||||
{
|
||||
catch (JMSException e) {
|
||||
e.printStackTrace(); // TODO better handling?
|
||||
}
|
||||
}
|
||||
|
@ -186,11 +175,15 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
|||
}
|
||||
|
||||
public void send(Destination destination, Message message) throws JMSException {
|
||||
if (producer == null) {
|
||||
producer = getSession().createProducer(null);
|
||||
producer.setDeliveryMode(deliveryMode );
|
||||
getProducer().send(destination, message);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Sent! to destination: " + destination + " message: " + message);
|
||||
}
|
||||
producer.send(destination, message);
|
||||
}
|
||||
|
||||
public void send(Destination destination, Message message, boolean persistent, int priority, int timeToLive) throws JMSException {
|
||||
int deliveryMode = persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT;
|
||||
getProducer().send(destination, message, deliveryMode, priority, timeToLive);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Sent! to destination: " + destination + " message: " + message);
|
||||
}
|
||||
|
@ -230,12 +223,23 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
|||
}
|
||||
}
|
||||
|
||||
public synchronized MessageProducer getProducer() throws JMSException {
|
||||
if (producer == null) {
|
||||
producer = getSession().createProducer(null);
|
||||
producer.setDeliveryMode(deliveryMode);
|
||||
}
|
||||
return producer;
|
||||
}
|
||||
|
||||
public void setProducer(MessageProducer producer) {
|
||||
this.producer = producer;
|
||||
}
|
||||
|
||||
public synchronized MessageConsumer getConsumer(Destination destination) throws JMSException {
|
||||
return getConsumer(destination, true);
|
||||
}
|
||||
|
||||
public synchronized MessageConsumer getConsumer(Destination destination, boolean create) throws JMSException {
|
||||
|
||||
MessageConsumer consumer = (MessageConsumer) consumers.get(destination);
|
||||
if (create && consumer == null) {
|
||||
consumer = getSession().createConsumer(destination);
|
||||
|
@ -255,8 +259,7 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
|||
}
|
||||
}
|
||||
|
||||
public synchronized List getConsumers()
|
||||
{
|
||||
public synchronized List getConsumers() {
|
||||
return new ArrayList(consumers.values());
|
||||
}
|
||||
|
||||
|
@ -264,7 +267,6 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
|||
return (ActiveMQSession) getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
}
|
||||
|
||||
|
||||
public Semaphore getSemaphore() {
|
||||
return semaphore;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue