如何使用java连接并向MQ发送数据

如何使用java连接并向MQ发送数据,java,jms,mq,Java,Jms,Mq,我已经在我的机器(imqbrokerd.exe)中运行了ActiveMQ,并获得了以下详细信息。我把我的机器名藏起来了 [#|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

我已经在我的机器(imqbrokerd.exe)中运行了ActiveMQ,并获得了以下详细信息。我把我的机器名藏起来了

[#|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.
|#]
[更新#1]:

当我使用下面的代码时,我得到下面的错误。我已附加了activeMQ队列详细信息的图像。我知道我使用的URL是错误的。你能帮我找个合适的吗

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

希望这有帮助。。 我已将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);
private static final String CONNECTION\u FACTORY\u NAME=“CONNECTION FACTORY NAME在此显示”;
私有静态最终字符串TOPIC\u NAME=“TOPIC NAME在此显示”;
私有静态最终字符串服务器\u URL\u PREFIX=“t3://”;
私有静态最终字符串服务器_URL_SUFFIX=“.URL.com:port”;
私有静态最终字符串USER=“”;
私有静态最终字符串密码=”;
私有静态最终字符串LOCAL_DIRECTORY=“C:\\tmp\\poslog\\”;
公共静态void main(字符串args[])引发JMSExException,
NamingException、IOException、InterruptedException{
System.out.println(“开始”+新日期());
//初始化
System.out.println(“为“+args[0]创建上下文”);
Hashtable属性=新的Hashtable();
properties.put(Context.INITIAL\u Context\u工厂,
“weblogic.jndi.WLInitialContextFactory”);
properties.put(Context.PROVIDER\u URL、SERVER\u URL\u前缀+args[0]+SERVER\u URL\u后缀);
//properties.put(Context.SECURITY\u主体,用户);
//properties.put(Context.SECURITY\u凭证、密码);
InitialContext ctx=新的InitialContext(属性);
TopicConnectionFactory connectionFactory=(TopicConnectionFactory)ctx
.查找(连接\工厂\名称);
TopicConnection=connectionFactory.createTopicConnection();
TopicSession会话=connection.createTopicSession(false,0);
Topic=(Topic)ctx.lookup(Topic\u NAME);
TopicPublisher-sender=session.createPublisher(主题);
希望这有帮助。。 我已将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);
private static final String CONNECTION\u FACTORY\u NAME=“CONNECTION FACTORY NAME在此显示”;
私有静态最终字符串TOPIC\u NAME=“TOPIC NAME在此显示”;
私有静态最终字符串服务器\u URL\u PREFIX=“t3://”;
私有静态最终字符串服务器_URL_SUFFIX=“.URL.com:port”;
私有静态最终字符串USER=“”;
私有静态最终字符串密码=”;
私有静态最终字符串LOCAL_DIRECTORY=“C:\\tmp\\poslog\\”;
公共静态void main(字符串args[])引发JMSExException,
NamingException、IOException、InterruptedException{
System.out.println(“开始”+新日期());
//初始化
System.out.println(“为“+args[0]创建上下文”);
Hashtable属性=新的Hashtable();
properties.put(Context.INITIAL\u Context\u工厂,
“weblogic.jndi.WLInitialContextFactory”);
properties.put(Context.PROVIDER\u URL、SERVER\u URL\u前缀+args[0]+SERVER\u URL\u后缀);
//properties.put(Context.SECURITY\u主体,用户);
//properties.put(Context.SECURITY\u凭证、密码);
InitialContext ctx=新的InitialContext(属性);
TopicConnectionFactory connectionFactory=(TopicConnectionFactory)ctx
.查找(连接\工厂\名称);
TopicConnection=connectionFactory.createTopicConnection();
TopicSession会话=connection.createTopicSession(false,0);
Topic=(Topic)ctx.lookup(Topic\u NAME);
TopicPublisher-sender=session.createPublisher(主题);

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

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

队列也是您需要在weblogic管理控制台上预定义/设置的内容。它是对驻留在weblogic上的队列的引用,您已经在前面进行了设置


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

JNDI_FACTORY更像是您要用于连接的驱动程序,它们通常特定于供应商,在您的情况下是weblogic,并且是预定义的

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

队列也是您需要在weblogic管理控制台上预定义/设置的内容。它是对驻留在weblogic上的队列的引用,您已经在前面进行了设置


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

请查看ActiveMQ文档。
t3://
是Oracle的专有协议(最初由Bea发明)。要使用它,您需要使用Weblogic客户端库。它不能用于与ActiveMQ代理通信。谢谢@SubOptimal。该页面确实有一些我一直在寻找的非常好的信息。您到底在寻找什么?@SubOptimal出现了一些错误,并更新了原始帖子中的详细信息。。有没有帮助兄弟?可能是其中的一个链接bri请看一下ActiveMQ文档。
t3://
是Oracle的专有协议(最初由Bea发明)。要使用它,您需要使用Weblogic客户端库。它不能用于与ActiveMQ代理通信。谢谢@SubOptimal。该页面确实有一些我一直在寻找的非常好的信息。您到底在寻找什么?@SubOptimal出现了一些错误,并更新了原始帖子中的详细信息。。有没有帮助兄弟?可能是其中的一个链接bri再亮一点。首先,很抱歉有这么长的距离。我刚把它捡回来,又一个错误很快落下
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);