上一篇针对TongLINK/Q的安装部署
,这篇具体说明java集成
1. 准备工作
根据实际修改连接URL等信息
vim TLQ8/etc/tlqjndi.conf
2. JAVA集成
将TongJMS.jar加载到自己的项目中
发送消息
import lombok.extern.slf4j.Slf4j;
import java.util.Properties;
import javax.jms.*;
@Slf4j
public class QueueSendMsg {
private static final String tcf = "tongtech.jms.jndi.JmsContextFactory";
private static final String remoteURL = "tlq://192.168.1.40:10024";
private static final String remoteFactory = "RemoteConnectionFactory";
public static void startSendMsg() {
ConnectionFactory ConnFactory = null;
Connection conn = null;
Session session = null;
Queue queue = null;
MessageProducer mProducer = null;
TextMessage testMessage = null;
try {
Properties pro = new Properties();
pro.setProperty("java.naming.factory.initial", tcf);
pro.setProperty("java.naming.provider.url", remoteURL);
javax.naming.Context ctx = new javax.naming.InitialContext(pro);
ConnFactory = (javax.jms.ConnectionFactory) ctx.lookup(remoteFactory);
queue = (javax.jms.Queue) ctx.lookup("MyQueue");
conn = ConnFactory.createConnection();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
mProducer = session.createProducer(queue);
testMessage = session.createTextMessage("hello world");
// 开启连接,并发送消息
conn.start();
log.info(" = = = = = = = = = 开始发送消息。。。");
mProducer.send(testMessage);
log.info(" = = = = = = = = = 消息发送完成。。。");
} catch (Exception e) {
log.info(" = = = = = = = = = 消息发送异常。。。" + e.toString());
e.printStackTrace();
} finally {
try {
if (session != null) {
session.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
log.info(" = = = = = = = = = 关闭消息连接时异常:" + e.toString());
e.printStackTrace();
}
}
}
public static void main(String[] args) {
startSendMsg();
}
}
使用main方法运行
接收消息
import lombok.extern.slf4j.Slf4j;
import javax.jms.*;
import javax.naming.Context;
import java.util.Properties;
@Slf4j
public class QueueGetMsg {
private static final String tcf = "tongtech.jms.jndi.JmsContextFactory";
private static final String remoteURL = "tlq://192.168.1.40:10024";
private static final String remoteFactory = "RemoteConnectionFactory";
public static void startGetMsg(){
ConnectionFactory connFactory = null;
Connection conn = null;
Session session = null;
Queue queue = null;
MessageConsumer consumer = null;
try {
Properties pro = new Properties();
pro.setProperty("java.naming.factory.initial", tcf);
pro.setProperty("java.naming.provider.url", remoteURL);
Context context = new javax.naming.InitialContext(pro);
connFactory = (ConnectionFactory) context.lookup(remoteFactory);
conn = connFactory.createConnection();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) context.lookup("MyQueue");
consumer = session.createConsumer(queue);
conn.start();
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
try {
if (message != null) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
log.info(" = = = = = = = = = 收到一条Text消息:" + textMessage.getText());
log.info(" = = = = = = = = = 来自 MyQueue :" + textMessage);
}
} else {
log.info(" = = = = = = = = = 消息为空 ");
}
} catch (JMSException e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
System.out.println("Exception" + e.toString());
log.info(" = = = = = = = = = 消息监听器异常: " + e.toString());
e.printStackTrace();
}
}
public static void main(String[] args) {
startGetMsg();
}
}
使用main方法运行