import com.SysUser;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ScheduledMessage;
import org.apache.activemq.command.ActiveMQDestination;
import org.springframework.util.ObjectUtils;
import javax.jms.*;
import java.io.Serializable;
public class ActiveMQUtils
{
public static final String BROKER_URL = "failover:(tcp://localhost:61616)";
public static String sendMessage(Session session, MessageProducer producer,String msgId,Object message,long delay) throws Exception {
SysUser sysUser = new SysUser();
sysUser.setUsername("777");
sysUser.setPassword("1234");
TextMessage tm2 = session.createTextMessage("Send Message After 10 seconds!");
ObjectMessage objectMessage2 = session.createObjectMessage((Serializable) message);
long delayTime = 3 * 10 * 1000;
objectMessage2.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
objectMessage2.setStringProperty("selector",msgId);
objectMessage2.setStringProperty("test","test2");
System.out.println("msgId : " + msgId);
producer.send(objectMessage2);
return msgId;
}
public static String sendMsg(String msgId,Object message,long delay,String destinationName) throws Exception {
Connection connection = null;
Session session = null;
try {
ConnectionFactory factory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);
((ActiveMQConnectionFactory) factory).setTrustAllPackages(true);
connection = factory.createConnection();
connection.start();
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(destinationName);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
msgId = sendMessage(session, producer,msgId,message,delay);
session.commit();
} catch (Exception e) {
throw e;
} finally {
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
}
return msgId;
}
public static void ClearMessage(String msgId,String destinationName)
{
ConnectionFactory connectionFactory;
Connection connection = null;
Session session;
Destination destination;
MessageConsumer consumer;
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
BROKER_URL);
((ActiveMQConnectionFactory) connectionFactory).setTrustAllPackages(true);
try {
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(destinationName);
consumer = session.createConsumer(destination,"selector='" + msgId+"'");
while (true) {
ObjectMessage message = (ObjectMessage) consumer.receive(10);
if (!ObjectUtils.isEmpty(message)) {
System.out.println("把消息拿下来但不处理! messageId = " + message.getStringProperty("selector"));
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
public static void ClearQueue(String url,String queueName)
{
ActiveMQConnection con = null;
try {
con = (ActiveMQConnection) new ActiveMQConnectionFactory(url).createConnection();
Destination queue=null;
queue = con.createSession(false, Session.AUTO_ACKNOWLEDGE).createQueue(queueName);
con.start();
con.destroyDestination((ActiveMQDestination) queue);
} catch (JMSException e)
{
e.printStackTrace();
} finally {
try
{
con.stop();
} catch(Exception e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
ClearMessage("991addd6dc624c1598d62d54bbf823a6","anjin1");
SysUser sysUser = new SysUser();
sysUser.setUsername("123");
SysUser sysUser2 = new SysUser();
sysUser2.setUsername("123");
cancelObjectMsg("e12adfb0a3fd413ebd637e9c69cc2e49");
}
public static void createTextMsg(String msg, String destinationName, Long delay) throws JMSException {
Connection connection = getConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(destinationName);
MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage(msg);
if (!ObjectUtils.isEmpty(delay)) {
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay.longValue());
}
producer.send(message);
closeAll(connection, session);
}
public static String createObjectMsg(Object objMsg, String destinationName, Long delay) throws JMSException {
String msgId = UUID.randomUUID().toString().replaceAll("-", "");
Connection connection = getConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(destinationName);
MessageProducer producer = session.createProducer(destination);
ObjectMessage message = session.createObjectMessage((Serializable) objMsg);
message.setStringProperty("selector", msgId);
if (!ObjectUtils.isEmpty(delay)) {
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay.longValue());
}
producer.send(message);
closeAll(connection, session);
return msgId;
}
public static void cancelTextMsg(String msg) throws JMSException {
Connection connection = getConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination requestBrowse = session.createTopic(ScheduledMessage.AMQ_SCHEDULER_MANAGEMENT_DESTINATION);
Destination browseDest = session.createTemporaryQueue();
MessageConsumer browser = session.createConsumer(browseDest);
connection.start();
MessageProducer producer = session.createProducer(requestBrowse);
TextMessage request = session.createTextMessage();
request.setStringProperty(ScheduledMessage.AMQ_SCHEDULER_ACTION,
ScheduledMessage.AMQ_SCHEDULER_ACTION_BROWSE);
request.setJMSReplyTo(browseDest);
producer.send(request);
Message scheduled = browser.receive();
while (scheduled != null) {
TextMessage textScheduled = (TextMessage) scheduled;
if (textScheduled.getText().equals(msg)) {
Message remove = getMessage(session, scheduled);
producer.send(remove);
}
scheduled = browser.receive();
}
closeAll(connection, session);
}
public static void cancelObjectMsg(String msgId) throws JMSException {
Connection connection = getConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination requestBrowse = session.createTopic(ScheduledMessage.AMQ_SCHEDULER_MANAGEMENT_DESTINATION);
Destination browseDest = session.createTemporaryQueue();
MessageConsumer browser = session.createConsumer(browseDest);
connection.start();
MessageProducer producer = session.createProducer(requestBrowse);
TextMessage request = session.createTextMessage();
request.setStringProperty(ScheduledMessage.AMQ_SCHEDULER_ACTION,
ScheduledMessage.AMQ_SCHEDULER_ACTION_BROWSE);
request.setJMSReplyTo(browseDest);
producer.send(request);
Message scheduled = browser.receive();
while (scheduled != null) {
ObjectMessage objectMessage = (ObjectMessage) scheduled;
String selector = objectMessage.getStringProperty("selector");
if (selector.equals(msgId)) {
Message remove = getMessage(session, scheduled);
producer.send(remove);
}
scheduled = browser.receive();
}
closeAll(connection, session);
}
private static Message getMessage(Session session, Message scheduled) throws JMSException {
Message remove = session.createMessage();
remove.setStringProperty(ScheduledMessage.AMQ_SCHEDULER_ACTION,
ScheduledMessage.AMQ_SCHEDULER_ACTION_REMOVE);
String scheduleId = scheduled.getStringProperty(ScheduledMessage.AMQ_SCHEDULED_ID);
System.out.println(scheduleId);
remove.setStringProperty(ScheduledMessage.AMQ_SCHEDULED_ID, scheduleId);
return remove;
}
private static Connection getConnection() throws JMSException {
ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);
((ActiveMQConnectionFactory) factory).setTrustAllPackages(true);
return factory.createConnection();
}
private static void closeAll(Connection connection, Session session) throws JMSException {
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
}
}