0

I am not able to inject a SLSB in another SLSB. Actually created 3 projects 1) created a EJB project with an MDB 2) created a EJB project with a stateless session bean for posting the message 3) created a EJB project with a stateless session bean for injecting the above session bean But while injecting I am not able to inject the EJB it is returning null

the code is as below 1) MDB:

@MessageDriven(
    activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "activemq/queue/TestQueue"),
        @ActivationConfigProperty(propertyName="acknowledgeMode", propertyValue="Auto-acknowledge")
    })
@ResourceAdapter("activemq-ra.rar")
public class ConsumerMDB implements MessageListener {

public void onMessage(Message message) {
    try {
         System.out.println("Queue: Received a TextMessage at " + new Date());
         TextMessage msg = (TextMessage) message;
         System.out.println("Message is : " + msg.getText());
    } catch (JMSException e) {
        e.printStackTrace();
    }

}

}

2) Session Bean 1

package com.springboard.session;

import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;

@Stateless
@LocalBean
public class ProducerSession implements ProducerSessionLocal {

@Resource(mappedName="java:jboss/activemq/QueueConnectionFactory")
public static QueueConnectionFactory factory;

@Resource(mappedName = "java:jboss/activemq/queue/TestQueue")
public static Queue queue;  

@Override
public void sendMessage(String msg) {
        System.out.println("****************Entering into method********************");

        try {
            System.out.println(queue.getQueueName());
            QueueConnection qConnection = factory.createQueueConnection();
            QueueSession qSession = qConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
            TextMessage message = qSession.createTextMessage();
            message.setText(msg);

            QueueSender qSender = qSession.createSender(queue);

            qSender.send(message);

            qSender.close();
            qSession.close();
            qConnection.close();
        } catch (JMSException e) {          
            e.printStackTrace();
        }

        System.out.println("****************Exiting into method********************");
}
}

and the interface is package com.springboard.session;

import javax.ejb.Local;

@Local
public interface ProducerSessionLocal {
public void sendMessage(String msg);
}

3) Second session bean to inject the first session

@Stateless
public class TestProducerLocalBean implements TestProducerLocalBeanLocal {

@EJB(mappedName = "java:global/ProducerSessionActiveMQ/ProducerSession!com.springboard.session.ProducerSessionLocal")
public ProducerSessionLocal producer;

public TestProducerLocalBean() {
    System.out.println("*************Testing Producer****************");
    if(producer!=null){
    producer.sendMessage("This Message is from SessionBean to Session Bean to MDB");
    }
    else{
        System.out.println("EJB is null");
    }
    System.out.println("**********End************************");

}

@Override
public void messageSend(String msg) {
    // TODO Auto-generated method stub

}

and for testing purpose used a class import javax.ejb.EJB;

import com.springboard.session.test.TestProducerLocalBean;


public class testEJB {

@EJB
public static TestProducerLocalBean local =new TestProducerLocalBean();

public static void main(String[] args) {        

}
}

At producer EJB always retuns null. With using servlet to inject ProducerSession i am able to do it. but injecting with another EJB i not able to get it. Could any one please help me out what i am missing

Thanks in advance

kalyani l
  • 1
  • 2

1 Answers1

0

It's incorrect to use initialization ... = new Xyz() when using injection because initialization of those fields is the responsibility of the container. You probably attempted that because you noticed that the field was null, and that's because injection (including @EJB) is not supported in the main class unless you use an application client container.

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
  • Thanks for the reply. Actually my requirement is to inject the SLSB1 in SLSB2 and SLSB1 has to post message to MDB & mesaage to be populated by the MDB. For tracking MDB i used Activemq. Queue can be seend but message listing not seen.The same thing works fine with Servlet but while injecting through another SLSB i am getting null.Its mainly because the EJB jars are deployed in separate containers and we have to make them talk to each other by adding some properties file. As i am newbie dont know to write the properties file. can anyone help with this regard. – kalyani l May 29 '13 at 06:51