![JAR search and dependency download from the Maven repository](/logo.png)
com.mycila.jms.MessageCreator Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2010 Mycila
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mycila.jms;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import java.io.Serializable;
import java.util.Map;
/**
* @author Mathieu Carbou ([email protected])
*/
final class MessageCreator {
private final Session session;
public MessageCreator(Session session) {
this.session = session;
}
public Message createMessage(BasicMessage extends Serializable> message) {
Message m = create(message);
try {
for (Map.Entry entry : message.headers.entrySet()) {
switch (entry.getKey()) {
case CorrelationID:
m.setJMSCorrelationID((String) entry.getValue());
break;
case Type:
m.setJMSType((String) entry.getValue());
break;
default:
throw new AssertionError("Unsupported attribute: " + entry.getKey());
}
}
for (Map.Entry e : message.properties.entrySet()) {
Object v = e.getValue();
if (v instanceof Boolean) m.setBooleanProperty(e.getKey(), (Boolean) v);
else if (v instanceof Byte) m.setByteProperty(e.getKey(), (Byte) v);
else if (v instanceof Short) m.setShortProperty(e.getKey(), (Short) v);
else if (v instanceof Integer) m.setIntProperty(e.getKey(), (Integer) v);
else if (v instanceof Float) m.setFloatProperty(e.getKey(), (Float) v);
else if (v instanceof Long) m.setLongProperty(e.getKey(), (Long) v);
else if (v instanceof Double) m.setDoubleProperty(e.getKey(), (Double) v);
else if (v instanceof String) m.setStringProperty(e.getKey(), (String) v);
else m.setObjectProperty(e.getKey(), v instanceof Serializable ? v : String.valueOf(v));
}
return m;
} catch (JMSException e) {
Utils.close(session);
throw new JMSClientException(e);
}
}
private Message create(BasicMessage extends Serializable> message) {
try {
Serializable o = message.getBody();
if (o instanceof String)
return session.createTextMessage(String.valueOf(o));
if (o instanceof Map) {
MapMessage jmsMessage = session.createMapMessage();
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy