怎样在jboss中部署一个jms的简单使用?

04-09-19 swy
有两个客户端:Receive.java(消息的接收者)、Send.java(消息的发出者)

Receive.java

import javax.naming.*;

import java.util.*;

import java.lang.*;

import javax.rmi.PortableRemoteObject;

import javax.jms.*;

import java.util.Properties;

import javax.rmi.PortableRemoteObject;

import javax.naming.InitialContext;

import javax.naming.Context;

import javax.naming.NamingException;

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.Session;

import javax.jms.TextMessage;

import javax.jms.Topic;

import javax.jms.TopicConnection;

import javax.jms.TopicConnectionFactory;

import javax.jms.TopicSubscriber;

import javax.jms.TopicSession;

import java.io.FileInputStream;

public class Receive implements MessageListener{

private static QueueReceiver receiver;

private static TextMessage msg;

private static final String url = "jnp://127.0.0.1:1099";//???????????????

static int EXPECTED_MESSAGE_COUNT = 2;

static int messageCount = 0;

public static void main(String[] args) throws Exception {

int MAX_TRIES = 10;

int tryCount = 0;

try{

Properties env = new Properties();

env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

//env.put(Context.PROVIDER_URL,url);

env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");

Context ctx = new InitialContext(env);

QueueConnectionFactory qConFactory = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");

QueueConnection qCon = qConFactory.createQueueConnection();

QueueSession session = qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

Queue messageQueue = (Queue)ctx.lookup("queue/testQueue");

receiver = session.createReceiver(messageQueue);

receiver.setMessageListener(new Receive());

qCon.start();

while(expectMoreMessages()&&(tryCount<MAX_TRIES)) {

try{

Thread.sleep(1000);

}catch(InterruptedException ignore){

tryCount++;

}

}

}catch(JMSException e){

e.printStackTrace();

}

}

public static boolean expectMoreMessages(){

return messageCount < EXPECTED_MESSAGE_COUNT;

}

public void onMessage(Message m) {

try

{

TextMessage msg = (TextMessage)m;

System.out.println("Received: " + msg.getText());

System.out.println("Received" + msg.getStringProperty("MyProp"));

}

catch (JMSException e)

{

e.printStackTrace();

}

}

}

Send.java

import javax.jms.*;

import javax.naming.*;

import java.io.*;

import java.util.*;

import java.rmi.RemoteException;

public class Send{

private static QueueSender sender;

private static TextMessage msg;

private static final String url = "localhost:1099";//??????????????

public static void main(String[] args) throws Exception {

try{

Hashtable env = new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

//env.put(Context.PROVIDER_URL,url);

env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");

Context ctx = new InitialContext(env);

QueueConnectionFactory qConFactory = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");

QueueConnection qCon = qConFactory.createQueueConnection();

QueueSession session = qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

Queue messageQueue = (Queue)ctx.lookup("queue/testQueue");

sender = session.createSender(messageQueue);

msg = session.createTextMessage();

msg.setText("Hello");

msg.setStringProperty("MyProp", "PropString 1");

sender.send(messageQueue,msg);

msg.setText("Welcome to JMS");

msg.setStringProperty("My Prop", "Prop String 2");

sender.send(messageQueue,msg);

}catch(NamingException ne){

System.err.println("The JMS Server and the appropriate " + "JMS Destination must be configured properly.");

}

}

}

我要在jboss中怎么部署我的这两个程序,才能让它们正常运行呢?

还是放在哪里都可以,请高手指点!!!

banq
2004-09-24 09:37
取决于你调用这两个代码的客户端形式,是Web还是EJB。

猜你喜欢