init
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package com.eactive.eai.common.activemq;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.JMSException;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
import org.apache.activemq.pool.PooledConnectionFactory;
|
||||
|
||||
import com.eactive.eai.common.property.PropManager;
|
||||
import com.eactive.eai.common.routing.RouteKeys;
|
||||
|
||||
public class AmqPooledConnectionFactory {
|
||||
public static String CONNECTION_URI = "vm://localhost?broker.persistent=false&broker.useJmx=true&jms.useAsyncSend=true&jms.prefetchPolicy.all=0";
|
||||
|
||||
private static final AmqPooledConnectionFactory instance = new AmqPooledConnectionFactory();
|
||||
|
||||
private PooledConnectionFactory pooledConnectionFactory;
|
||||
|
||||
public static AmqPooledConnectionFactory getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private AmqPooledConnectionFactory() {
|
||||
createNewFactory();
|
||||
}
|
||||
|
||||
public Connection getConnection() throws JMSException {
|
||||
return pooledConnectionFactory.createConnection();
|
||||
}
|
||||
|
||||
private void createNewFactory() {
|
||||
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(CONNECTION_URI);
|
||||
connectionFactory.setTrustAllPackages(true);
|
||||
|
||||
// Create a PooledConnectionFactory and set some configuration options
|
||||
pooledConnectionFactory = new PooledConnectionFactory();
|
||||
pooledConnectionFactory.setConnectionFactory(connectionFactory);
|
||||
|
||||
setConnectionProperties();
|
||||
|
||||
pooledConnectionFactory.start();
|
||||
}
|
||||
|
||||
private PooledConnectionFactory setConnectionProperties() {
|
||||
Properties props = PropManager.getInstance().getProperties(RouteKeys.GROUP_NAME);
|
||||
int maxConnection = Integer
|
||||
.parseInt(props.getProperty(RouteKeys.ROUTING_QUEUE_CONNECTION_FACTORY_MAXCONNECTION, "5"));
|
||||
|
||||
long expiryTimeout = Long
|
||||
.parseLong(props.getProperty(RouteKeys.ROUTING_QUEUE_CONNECTION_FACTORY_EXPIRYTIMEOUT, "0"));
|
||||
|
||||
int idleTimeout = Integer
|
||||
.parseInt(props.getProperty(RouteKeys.ROUTING_QUEUE_CONNECTION_FACTORY_IDLETIMEOUT, "30000"));
|
||||
|
||||
pooledConnectionFactory.setMaxConnections(maxConnection);
|
||||
pooledConnectionFactory.setIdleTimeout(idleTimeout);
|
||||
pooledConnectionFactory.setExpiryTimeout(expiryTimeout);
|
||||
|
||||
return pooledConnectionFactory;
|
||||
}
|
||||
|
||||
public PooledConnectionFactory reload() {
|
||||
return setConnectionProperties();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.eactive.eai.common.activemq;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.DeliveryMode;
|
||||
import javax.jms.Destination;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.MessageProducer;
|
||||
import javax.jms.ObjectMessage;
|
||||
import javax.jms.Session;
|
||||
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class AmqQueueProducer {
|
||||
|
||||
private Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
private String queueName = null;
|
||||
|
||||
public AmqQueueProducer(String connectionUrl, String queueName) {
|
||||
this.queueName = queueName;
|
||||
}
|
||||
|
||||
public void sendMessage(Serializable object, Properties prop) throws JMSException {
|
||||
Connection connection = null;
|
||||
Session session = null;
|
||||
MessageProducer producer = null;
|
||||
|
||||
try {
|
||||
// Create a Connection
|
||||
connection = AmqPooledConnectionFactory.getInstance().getConnection();
|
||||
|
||||
// Create a Session
|
||||
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
|
||||
// Create the destination (Topic or Queue)
|
||||
Destination destination = session.createQueue(queueName);
|
||||
producer = session.createProducer(destination);
|
||||
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
|
||||
ObjectMessage message = session.createObjectMessage(object);
|
||||
if (prop != null) {
|
||||
for(Object key : prop.keySet()) {
|
||||
message.setObjectProperty(key.toString(), prop.get(key));
|
||||
}
|
||||
}
|
||||
producer.send(message);
|
||||
} catch (JMSException ex) {
|
||||
logger.error("Send Error = " + ex.getMessage(), ex);
|
||||
throw ex;
|
||||
} finally {
|
||||
if (producer != null) {
|
||||
try {
|
||||
producer.close();
|
||||
} catch (Exception e) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
if (session != null)
|
||||
try {
|
||||
session.close();
|
||||
} catch (JMSException e) {
|
||||
// ignored
|
||||
}
|
||||
if (connection != null)
|
||||
try {
|
||||
connection.close();
|
||||
} catch (JMSException e) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.eactive.eai.common.activemq;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Iterator;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.DeliveryMode;
|
||||
import javax.jms.Destination;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.MessageProducer;
|
||||
import javax.jms.ObjectMessage;
|
||||
import javax.jms.Session;
|
||||
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class AmqTopicProducer {
|
||||
|
||||
Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
Connection connection = null;
|
||||
Session session = null;
|
||||
MessageProducer producer = null;
|
||||
String connectionUrl = null;
|
||||
String topicName = null;
|
||||
String correlationID = null;
|
||||
|
||||
public AmqTopicProducer(String connectionUrl, String topicName, String correlationID) {
|
||||
this.connectionUrl = connectionUrl;
|
||||
this.topicName = topicName;
|
||||
this.correlationID = correlationID;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void sendMessage(Serializable object, Properties prop, long ttl) throws JMSException {
|
||||
try {
|
||||
// Create a Connection
|
||||
connection = AmqPooledConnectionFactory.getInstance().getConnection();
|
||||
|
||||
// Create a Session
|
||||
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
|
||||
// Create the destination (Topic or Queue)
|
||||
Destination destination = session.createTopic(topicName);
|
||||
producer = session.createProducer(destination);
|
||||
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
|
||||
ObjectMessage message = session.createObjectMessage(object);
|
||||
|
||||
if(ttl > 0) {
|
||||
producer.setTimeToLive(ttl);
|
||||
}
|
||||
|
||||
if (correlationID != null) {
|
||||
message.setJMSCorrelationID(correlationID);
|
||||
}
|
||||
|
||||
if (prop != null) {
|
||||
Iterator it = prop.keySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
String key = (String) it.next();
|
||||
String value = prop.getProperty(key);
|
||||
message.setStringProperty(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
producer.send(message);
|
||||
}
|
||||
catch(JMSException ex) {
|
||||
logger.error("Send Error = " + ex.getMessage(), ex);
|
||||
throw ex;
|
||||
}
|
||||
finally {
|
||||
if (producer != null) {
|
||||
try {
|
||||
producer.close();
|
||||
} catch (Exception e) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
if (session != null)
|
||||
try {
|
||||
session.close();
|
||||
} catch (JMSException e) {
|
||||
// ignored
|
||||
}
|
||||
if (connection != null)
|
||||
try {
|
||||
connection.close();
|
||||
} catch (JMSException e) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user