try {
connection = cf.createConnection(userName, password);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(dest);
ActiveMQTextMessage msg = (ActiveMQTextMessage) session.createTextMessage(body);
for (Iterator iter = headers.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
msg.setObjectProperty((String) entry.getKey(), entry.getValue());
}
producer.setDeliveryMode(msg.getJMSDeliveryMode());
producer.setPriority(msg.getPriority());
long ttl = msg.getExpiration() - System.currentTimeMillis();
producer.setTimeToLive(ttl > 0 ? ttl : 0);
producer.send(msg);
return msg.getJMSMessageID();
} finally {
connection.close();
}