Hey listen ,
i am giving u some code that connects to the JBOSS-MQ and publishes
some messages to the queue-D .
If u want to connect to some other Appserver then u need to change
only the initial context factory i.e ( the getProperties()- method in
the code.
to run the code u need to have some jar in the CLASSPATH and run the
Jboss server & then run the Code .
The code is as follows :
--------------------------------------------------------------
public class JMSPublisher {
public JMSPublisher() {
super();
}
public void testConnection(){
System.out.println("Before getting the proiperties..");
Hashtable properties = getProperties();
System.out.println("************Created the Environmental peopertiers**********");
Context context = null;
QueueConnectionFactory queueFactory =null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueSender sender = null;
// Get the initial context with given properties
System.out.println("Before creating the Initial Context..");
try {
context = new InitialContext( properties);
} catch (NamingException e) {
System.out.println("Exception while gettin the Context");
e.printStackTrace();
}
//Get the connection factory
try {
queueFactory = (QueueConnectionFactory)context.lookup("ConnectionFactory");
System.out.println("Received the Factory from context..");
} catch (NamingException e) {
System.out.println("Exception while getting the Factory");
e.printStackTrace();
}
try {
queueConnection = queueFactory.createQueueConnection();
System.out.println("Created the Queue Connection ..");
} catch (JMSException e) {
System.out.println("Exception while Creating the QueueConnection");
e.printStackTrace();
}
try {
queueSession = queueConnection.createQueueSession( false, Session.AUTO_ACKNOWLEDGE);
System.out.println("Created the Session On the created connection ..");
} catch (JMSException e) {
System.out.println("Exception while Creating the Session On the created connection ..");
e.printStackTrace();
}
try {
queue = queueSession.createQueue("D");
System.out.println("Created the Queue on the session");
} catch (JMSException e) {
System.out.println("Exception while creating the Queue On session ..");
e.printStackTrace();
}
try {
sender = queueSession.createSender( queue );
System.out.println("the Sender Has been created to the
Test Queue");
} catch (JMSException e) {
System.out.println("Exception while creating the Sender");
e.printStackTrace();
}
TextMessage message = null;
int i = 1;
while( i<=2){
try {
message = queueSession.createTextMessage();
message.setText("Message :::" + i ) ;
message.setJMSDeliveryMode( DeliveryMode.PERSISTENT);
sender.send( message );
} catch (JMSException e) {
System.out.println("Exception while creating the Test Message..");
e.printStackTrace();
}
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Hashtable getProperties(){
Hashtable props = new Hashtable();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(Context.PROVIDER_URL, "127.0.0.1:1099");
props.put("java.naming.rmi.security.manager", "yes");
props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");
return props;
}
}