简体   繁体   English

如何使用Java连接数据并将数据发送到MQ

[英]How to connect and send data to MQ with java

I've run ActiveMQ in my machine (imqbrokerd.exe) and got below details. 我已经在计算机(imqbrokerd.exe)中运行ActiveMQ,并获得了以下详细信息。 I've hidden my machine name with 我已经用隐藏了机器名称

[#|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.
|#]

And I'm using below java program to connect to this queue. 我正在使用下面的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();

    }

}

question here... 在这里提问...

a. 一种。 What should be the values in JNDI_FACTORY, JMS_FACTORY, QUEUE, url and what do they signify? JNDI_FACTORY,JMS_FACTORY,QUEUE,URL中的值应该是什么?它们分别表示什么? b. What does 't3://' in url means? 网址中的“ t3://”是什么意思? Is this a protocol ? 这是协议吗? if so, what should be given for active MQ? 如果是这样,应该为活动的MQ提供什么?

FYI, i'm getting below error 仅供参考,我正在错误以下

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

[UPDATE #1]: [更新#1]:

When I use below code, I get below error. 当我使用以下代码时,出现以下错误。 I've attached images of my activeMQ queue details. 我已附上我的activeMQ队列详细信息的图像。 I know that the URL that i'm using is wrong. 我知道我使用的URL错误。 Can you please help me out with the right one? 您能帮我选对一个吗?

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();

在此处输入图片说明

在此处输入图片说明

Hope this Helps.. I have passed the JMS server name as an argument. 希望对您有所帮助。我已将JMS服务器名称作为参数传递。 And WLS library jar should be imported 而且应该导入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);

JNDI_FACTORY is more like a driver that you want to use to connect, they are usually specific to the vendor in your case is weblogic and is pre-defined. JNDI_FACTORY更像是您要用来连接的驱动程序,它们通常是特定于供应商的,如果您使用的是weblogic并且是预定义的。

JMS_FACTORY is the connection factory that you have already predefined in web logic for this type of integration. JMS_FACTORY是您已经在Web逻辑中为这种类型的集成预定义的连接工厂。 it is responsible for managing the connections to the queue. 它负责管理与队列的连接。

QUEUE also is something that you need to predefine / setup on weblogic admin console. 您还需要在Weblogic管理控制台上预先定义/设置QUEUE。 It is the reference to the queues that reside on weblogic which you have setup before hand. 它是对预先设置的位于weblogic上的队列的引用。

t3 is the connection type you are using, the other alternative could be iiop. t3是您正在使用的连接类型,另一种选择可能是iiop。 t3 it is a more lightweight type of connection. t3是一种更轻量的连接类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM