2016-05-06 3 views
0

J'utilise ActiveMq comme fournisseur et API JMS 2.0. La deuxième ligne lance AbstractMethodError, comment y remédier?Dans JMS 2.0 factory.createContext() lance 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)

Le destination objet créé à l'aide des étapes suivantes.

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

Vous trouverez ci-dessous un extrait complet de 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); 
    } 

} 
} 

Répondre

1

Si ma mémoire est correcte, activemq ne prend pas encore en charge JMS2. (le problème lié dans leur JIRA est toujours ouvert: https://issues.apache.org/jira/browse/AMQ-5383)

La méthode createContext() ne donnera donc pas les résultats attendus. Cependant, vous pouvez toujours utiliser la méthode createConnection() pour faire le travail puisque JMS2 a la compatibilité descendante de JMS 1.1.

+0

Merci Vaaith, j'aurais dû vérifier le support Activemq pour jms2.0. – ramanKC

+0

ActiveMQ: pas de prise en charge de JMS 2.0 IBM MQ version 8.0: prend en charge JMS 2.0 – ramanKC