简体   繁体   English

在JMS 2.0中,factory.createContext()引发AbstractMethodError

[英]In JMS 2.0 factory.createContext() is throwing AbstractMethodError

I am using ActiveMq as provider and JMS 2.0 API. 我正在使用ActiveMq作为提供程序和JMS 2.0 API。 The second line is throwing AbstractMethodError , how to fix it? 第二行抛出AbstractMethodError ,如何解决?

1. ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
2. JMSContext context = factory.createContext();
3. context.createProducer().send(destination, msg);

Exception in thread "main" java.lang.AbstractMethodError: org.apache.activemq.ActiveMQConnectionFactory.createContext()Ljavax/jms/JMSContext; at jms.advance.Sender.sendMessage(Sender.java:41) at jms.advance.Sender.main(Sender.java:26)

The destination object I created using following steps. 我使用以下步骤创建的目标对象。

ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);   
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("SAMPLE_test_QUEUE");

Below is complete snippet of code. 以下是完整的代码段。

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.JMSRuntimeException;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Sender {
    public String senderId;
    private ConnectionFactory factory = null;
    private Connection connection = null;
    private Session session = null;
    private Destination destination = null;

public static void main(String... arg) throws JMSException, InterruptedException{
    Sender sender = new Sender("facebook");
    int count = 1;
    for(int i=0; i <= 100; i++, count++){
        String msg = sender.senderId +": "+ "sending msg #" + count;
        sender.sendMessage(sender.factory, sender.destination, msg);
        Thread.sleep(3000);
    }
}
public Sender(String senderid) throws JMSRuntimeException, JMSException {
    this.senderId = senderid;
    factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
    connection = factory.createConnection();
    connection.start();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    destination = session.createQueue("SAMPLE_test_QUEUE");

}

public void sendMessage(ConnectionFactory factory, Destination destination, String msg) throws JMSException{
    try(JMSContext context = factory.createContext()){
        context.createProducer().send(destination, msg);
    }catch(JMSRuntimeException e)
    {
        System.out.println(e);
    }

}
}

If my memory is correct, activemq does not yet support JMS2. 如果我的内存正确,则activemq尚不支持JMS2。 (the related issue in their JIRA is still open : https://issues.apache.org/jira/browse/AMQ-5383 ) (他们的JIRA中的相关问题仍未解决: https : //issues.apache.org/jira/browse/AMQ-5383

So the createContext() method will not yield the expected results. 因此, createContext()方法将不会产生预期的结果。 However you can still use the method createConnection() to do the work since JMS2 has JMS 1.1 backward compatibility. 但是,由于JMS2具有JMS 1.1向后兼容性,您仍然可以使用方法createConnection()来完成工作。

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

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