eclipse中的Java EE项目文件夹结构?

时间:2012-11-19 05:19:00

标签: java eclipse java-ee jboss jms

我是Java EE编程的新手。我正在尝试使用Java EE在Eclipse中编写JMS发送方。我创建了一个新的Java EE项目,现在我怀疑的是文件夹结构是什么以及在哪里编写我的JMS发送者java文件。

我正在使用JBoss AS。我的要求是我必须使用此JMS发送器应用程序将消息推送到JMS队列。

1 个答案:

答案 0 :(得分:1)

首先,您需要编写一个MDB(消息驱动的bean)来使用您的JMS消息。为清楚起见,我将提供一个小例子;

@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue =   "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/offlinemsg"),
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class MessageHandlerMDB implements MessageListener {

private static final Logger log = Logger.getLogger(MessageHandlerMDB.class);


@Override
public void onMessage(Message message) {

    log.info("Message received in MessageHandlerMDB");


}

}

在部署应用程序时,服务器将根据注释识别消息驱动Bean并为您创建队列。然后,您可以像往常一样通过ConnectionFactory将消息发送到此队列。如果您通过Jboss中的同一个应用程序发送消息,那么您可以写如下:

 private void sendObjectToQueue(Serializable object) {


    Session session = null;
    MessageProducer sender = null;
    Connection connect = null;

    if (logger.isDebugEnabled()) {
    logger.debug("Entered sendObjectToQueue(" + object.getClass());
    }

    try {

        Context context = new InitialContext();
        ConnectionFactory factory = (ConnectionFactory) context
                .lookup("ConnectionFactory");

        Queue queue = (Queue) context.lookup("queue/offlinemsg");
        connect = factory.createConnection();
        session = connect.createSession(false,
                Session.AUTO_ACKNOWLEDGE);
        sender = session.createProducer(queue);

        ObjectMessage message = session.createObjectMessage();

        message.setObject(object);
        sender.send(message);
        sender.close();
        connect.close();

        if (logger.isDebugEnabled()) {
        logger.debug("The " + object.getClass() + " object was sent to jms queue");
        }

    } catch (JMSException e) {
        logger.error("Sending object to queue failed", e);


    } catch (Exception e) {
        logger.error("Sending object to queue failed", e);


    } finally {

        if (sender != null) {
            try {
                sender.close();
                if (logger.isDebugEnabled()) {
                    logger.debug("Closed jms session");
                }
            } catch (JMSException e) {
                logger.error("Closing jms session failed", e);
            }
        }

        if (connect != null) {
            try {
                connect.close();
                if (logger.isDebugEnabled()) {
                    logger.debug("Closed jms session");
                }
            } catch (Exception e) {
                logger.error("Closing jms session failed", e);
            }
        }

        if (session != null) {
            try {
                session.close();
                if (logger.isDebugEnabled()) {
                    logger.debug("Closed jms session");
                }
            } catch (Exception e) {
                logger.error("Closing jms session failed", e);
            }
        }
    }
}

如果您需要进一步澄清,请与我们联系。希望这会有所帮助。

被修改 *

如果您从普通的java类调用,则需要以下代码(请注意,这是特定于JBoss)

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

Context ctx = new InitialContext(env);
ConnectionFactory cf = (ConnectionFactory)ctx.lookup("/ConnectionFactory");
Queue queue = (Queue)ic.lookup("/queues/MyQueue");
Connection connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
connection.start();
TextMessage message = session.createTextMessage("Test");
producer.send(message);

请注意,您应该将连接创建和会话创建重用为最佳实践...