信创改造2---java集成TongLink/Q

上一篇针对TongLINK/Q的安装部署

http://t.csdnimg.cn/9ss7l

,这篇具体说明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方法运行 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值