如何使用java连接和发送数据到MQ

时间:2015-10-01 13:56:57

标签: java jms mq

我已经在我的机器上运行ActiveMQ(imqbrokerd.exe)并获得了以下详细信息。我用

隐藏了我的机器名称
[#|2015-10-01T19:16:06.788+0530|WARNING|5.1|imq.log.Logger|_ThreadID=1;_ThreadNa
me=main;|[S2004]: Log output channel com.sun.messaging.jmq.util.log.SysLogHandle
r is disabled: no imqutil in java.library.path|#]


[#|2015-10-01T19:16:06.804+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|
================================================================================

Message Queue 5.1
Oracle
Version:  5.1  (Build 9-b)
Compile:  July 29 2014 1229

Copyright (c) 2013, Oracle and/or its affiliates.  All rights reserved.
================================================================================

Java Runtime: 1.7.0_40 Oracle Corporation C:\Program Files (x86)\Java\jre7
|#]


[#|2015-10-01T19:16:06.819+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|   IMQ_HOME=C:\MessageQueue5.1\mq
|#]


[#|2015-10-01T19:16:06.819+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|IMQ_VARHOME=C:\MessageQueue5.1\var\mq
|#]


[#|2015-10-01T19:16:06.819+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|Windows 7 6.1 x86 <MachineName> (4 cpu) 

|#]


[#|2015-10-01T19:16:06.835+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|Java Heap Size: max=190080k, current=15872k
|#]


[#|2015-10-01T19:16:06.835+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|Arguments:
|#]


[#|2015-10-01T19:16:06.850+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|[B1060]: Loading persistent data...
|#]


[#|2015-10-01T19:16:06.866+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|Using built-in file-based persistent store: C:\MessageQueue5.1\var\mq\ins
tances\imqbroker\
|#]


[#|2015-10-01T19:16:07.194+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|[B1270]: Processing messages from transaction log file...
|#]


[#|2015-10-01T19:16:07.396+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|[B1039]: Broker "imqbroker@<MachineName>:7676"
ready.
|#]

我正在使用以下java程序连接到此队列。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class QueueSendLinear {


    public static void main(String args[]) throws JMSException, NamingException {
         // Defines the JNDI context factory.
         final String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

         // Defines the JMS context factory.
         final String JMS_FACTORY="jms/TestConnectionFactory";

         // Defines the queue.
         final String QUEUE="jms/TestJMSQueue";

         QueueConnectionFactory qconFactory;
         QueueConnection qcon;
         QueueSession qsession;
         QueueSender qsender;
         Queue queue;
         TextMessage msg;
         String xml = "Sample XML comes here!! ";

         String url = "t3://<MachineName>:7676";
         Hashtable env = new Hashtable();
         env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
         env.put(Context.PROVIDER_URL, url);
         InitialContext ic =  new InitialContext(env);

         qconFactory = (QueueConnectionFactory) ic.lookup(JMS_FACTORY);
         qcon = qconFactory.createQueueConnection();
         qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
         queue = (Queue) ic.lookup(QUEUE);
         qsender = qsession.createSender(queue);
         msg = qsession.createTextMessage();
         qcon.start();
         msg.setText(xml);
         qsender.send(msg);
         qsender.close();
         qsession.close();
         qcon.close();

    }

}

在这里提问......

一个。 JNDI_FACTORY,JMS_FACTORY,QUEUE,url中的值应该是什么以及它们表示什么? 湾什么&#39; t3://&#39;在url中意味着什么?这是一个协议吗?如果是这样,应该为主动MQ提供什么?

仅供参考,我收到以下错误

Oct 01, 2015 7:20:58 PM com.sun.corba.se.impl.naming.namingutil.CorbalocURL badAddress
WARNING: "IOP00110603: (BAD_PARAM) Bad host address in -ORBInitDef"
org.omg.CORBA.BAD_PARAM:   vmcid: SUN  minor code: 603  completed: No

[更新#1]:

当我使用下面的代码时,我得到以下错误。我已经附加了activeMQ队列详细信息的图像。我知道我使用的网址是错误的。你能帮我解决一个问题吗?

Exception in thread "main" javax.naming.NamingException: Couldn't connect to the specified host :  [Root exception is org.omg.CORBA.COMM_FAILURE:   vmcid: SUN  minor code: 208 completed: Maybe]
    at weblogic.corba.j2ee.naming.Utils.wrapNamingException(Utils.java:83)
    at weblogic.corba.j2ee.naming.ORBHelper.getORBReferenceWithRetry(ORBHelper.java:656)

final String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
final String JMS_FACTORY="jms/?";
final String QUEUE = "mq.sys.dmq"; 
QueueConnectionFactory qconFactory;
QueueConnection qcon;
QueueSession qsession;
QueueSender qsender;
Queue queue;
TextMessage msg;
String xml = "Sample XML comes here!! ";
String url =  "t3://localhost:51010";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
InitialContext ic =  new InitialContext(env);
qconFactory = (QueueConnectionFactory) ic.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ic.lookup(QUEUE);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

希望这有助于...... 我已将JMS服务器名称作为参数传递。并且应该导入WLS库jar

private static final String CONNECTION_FACTORY_NAME ="connection factory name goes here";  

private static final String TOPIC_NAME = "Topic Name goes here";


private static final String SERVER_URL_PREFIX = "t3://";
private static final String SERVER_URL_SUFFIX = ".url.com:port";
private static final String USER = "";
private static final String PASSWORD = "";

private static final String LOCAL_DIRECTORY = "C:\\tmp\\poslog\\";


public static void main(String args[]) throws JMSException,
        NamingException, IOException, InterruptedException {

    System.out.println("start" + new Date());
    // INITIALIZE
    System.out.println("creating context for " + args[0]);
    Hashtable<String, String> properties = new Hashtable<String, String>();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,
            "weblogic.jndi.WLInitialContextFactory");
    properties.put(Context.PROVIDER_URL, SERVER_URL_PREFIX + args[0] +      SERVER_URL_SUFFIX);
//properties.put(Context.SECURITY_PRINCIPAL, USER);
//properties.put(Context.SECURITY_CREDENTIALS, PASSWORD);
    InitialContext ctx = new InitialContext(properties);
    TopicConnectionFactory connectionFactory = (TopicConnectionFactory) ctx
            .lookup(CONNECTION_FACTORY_NAME);
    TopicConnection connection = connectionFactory.createTopicConnection();
    TopicSession session = connection.createTopicSession(false, 0);
    Topic topic = (Topic) ctx.lookup(TOPIC_NAME);
    TopicPublisher sender = session.createPublisher(topic);

答案 1 :(得分:0)

JNDI_FACTORY更像是您想要用于连接的驱动程序,它们通常特定于您的案例中的供应商是weblogic并且是预定义的。

JMS_FACTORY是您已在Web逻辑中为此类集成预定义的连接工厂。它负责管理与队列的连接。

QUEUE也是您需要在weblogic管理控制台上预定义/设置的东西。它是对您之前设置的weblogic上的队列的引用。

t3是您正在使用的连接类型,另一种选择可能是iiop。 t3它是一种更轻量级的连接。

相关问题