扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
如下的内容是关于Java调用ActiveMQ简单范例的内容,应该能对大伙有所用。
员工经过长期磨合与沉淀,具备了协作精神,得以通过团队的力量开发出优质的产品。成都创新互联坚持“专注、创新、易用”的产品理念,因为“专注所以专业、创新互联网站所以易用所以简单”。公司专注于为企业提供做网站、网站制作、微信公众号开发、电商网站开发,小程序设计,软件按需网站制作等一站式互联网企业服务。package jms;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.log4j.PropertyConfigurator;
public class PTP_sends {
public PTP_sends(){
}
private Connection connection;
private String subject = "TOOL.DEFAULT";
public void send(String msg){
try {
connection=connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination=session.createQueue(subject);
MessageProducer producer=session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = session.createTextMessage(msg);
producer.send(message);
System.out.println("消息已经发送。。。。");
message.clearProperties();
session.close();
connection.stop();
connection.close();
System.out.println("关闭资源。。。。");
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
PropertyConfigurator.configure("c:\log4j.properties");
PTP_sends ptpSends=new PTP_sends();
ptpSends.send("this is JMS .....");
}
}
package jms;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.PropertyConfigurator;
public class PTP_receive {
private Connection connection;
private String subject = "TOOL.DEFAULT";
public void receive(){
ActiveMQConnectionFactory connectionFactory =new ActiveMQConnectionFactory();
try {
connection=connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination=session.createQueue(subject);
MessageConsumer consumer=session.createConsumer(destination);
System.out.println("同步接受消息:");
Message message=consumer.receive();
System.out.println("n收到的message 是:"+((TextMessage)message).getText());
message.clearProperties();
consumer.close();
session.close();
connection.stop();
connection.close();
System.out.println("关闭资源。。。。");
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
PropertyConfigurator.configure("c:\log4j.properties");
PTP_receive receive=new PTP_receive();
receive.receive();
}
}
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流