/*
I'm trying to get an applet send and receive JMS messages
and I'm very close but sort of stuck. Here are my comments
as I've tried to work it out, followed by my test client.
Thanks for taking a look - Sam
A test client to Publish and Subscribe to a JMS topic.
It publishes messages to a topic and then receives a
copy back through its subscription to that topic.
It's having trouble when run as an applet but is fine
as an application. As an applet it can send messages
fine but it cannot subscribe. If it does no messages
are delivered, instead Sun's J2EE implementation logs
an exception in trying to deliver the message and the
next message published returns an error that the client
has been dropped by the JMS provider.
javax.jms.JMSException: No Connection found for pclientId:
vail1003257580276_7, connectionId 1
I also get an unidentified error before making the connection
to the JMS server when running as an applet and it is most
likely very related to my problem.
Ignoring com/sun/jms/util/jms.properties due to exception java.lang.NullPointerException
Java(TM) Message Service 1.0.2 Reference Implementation (build b13)
> java -version
java version "1.3.1_01"
> ls -al /opt/j2ee
/opt/j2ee -> /h/java/j2sdkee1.3/
I've been trying to run it in the appletviewer of a
Sun Blade (Solaris 8 for Blade).
appletviewer -J-Djava.security.policy=launch.policy
-J-Djms.properties=$J2EE_HOME/config/jms_client.properties Applet.html
I set the security policy to allow for the InitialContext
lookup.
> less launch.policy
grant codeBase "file:/-" {
permission java.security.AllPermission;
};
> less Applet.html
<APPLET CODE = "DynamicClient.class"
ARCHIVE = "j2ee.jar"
WIDTH = 200 HEIGHT = 50>
<PARAM NAME="com.sun.jms.internal.java.naming.factory.initial"
VALUE="com.sun.enterprise.naming.SerialInitContextFactory">
</APPLET>
(I have a copy of the j2ee.jar in the local directory)
I'm running this on my local machine with the applet class
in the classpath that I'm running the appletviewer from.
> echo $CLASSPATH
.:/opt/j2ee/lib/j2ee.jar:/opt/j2ee/lib/locale
The error reported in j2ee's jms log is
less /opt/j2ee/logs/vail/jms/jms/error.log
WARN Could not send message to client due to exception msg: ID:_vail_1003257580276_7.1.2.1: contains a java.lang.String
WARN Exception
Message:
org.omg.CORBA.UNKNOWN: minor code: 1398079690 completed: Maybe
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Class.java:237)
at com.sun.corba.ee.internal.iiop.messages.ReplyMessage_1_2.getSystemException(ReplyMessage_1_2.java:92)
at com.sun.corba.ee.internal.iiop.ClientResponseImpl.getSystemException(ClientResponseImpl.java:108)
at com.sun.corba.ee.internal.corba.ClientDelegate.invoke(ClientDelegate.java:281)
at org.omg.CORBA.portable.ObjectImpl._invoke(ObjectImpl.java:459)
at org.omg.stub.com.sun.jms._JMSClient_Stub.sendMessage(Unknown Source)
at com.sun.jms.service.ConnectionImpl.sendMessage(ConnectionImpl.java:480)
at com.sun.jms.service.SessionImpl.processOutgoingMessages(SessionImpl.java:1300)
at com.sun.jms.service.SessionImpl.access$000(SessionImpl.java:50)
at com.sun.jms.service.SessionImpl$1.activityDetected(SessionImpl.java:1019)
at com.sun.jms.util.WorkerThread.run(WorkerThread.java:92)
WARN Ignoring remote exception CORBA UNKNOWN 1398079690 Maybe; nested exception is:
org.omg.CORBA.UNKNOWN: minor code: 1398079690 completed: Maybe
WARN Exception
Message: CORBA UNKNOWN 1398079690 Maybe; nested exception is:
org.omg.CORBA.UNKNOWN: minor code: 1398079690 completed: Maybe
java.rmi.RemoteException: CORBA UNKNOWN 1398079690 Maybe; nested exception is:
org.omg.CORBA.UNKNOWN: minor code: 1398079690 completed: Maybe
org.omg.CORBA.UNKNOWN: minor code: 1398079690 completed: Maybe
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Class.java:237)
at com.sun.corba.ee.internal.iiop.messages.ReplyMessage_1_2.getSystemException(ReplyMessage_1_2.java:92)
at com.sun.corba.ee.internal.iiop.ClientResponseImpl.getSystemException(ClientResponseImpl.java:108)
at com.sun.corba.ee.internal.corba.ClientDelegate.invoke(ClientDelegate.java:281)
at org.omg.CORBA.portable.ObjectImpl._invoke(ObjectImpl.java:459)
at org.omg.stub.com.sun.jms._JMSClient_Stub.sendMessage(Unknown Source)
at com.sun.jms.service.ConnectionImpl.sendMessage(ConnectionImpl.java:480)
at com.sun.jms.service.SessionImpl.processOutgoingMessages(SessionImpl.java:1300)
at com.sun.jms.service.SessionImpl.access$000(SessionImpl.java:50)
at com.sun.jms.service.SessionImpl$1.activityDetected(SessionImpl.java:1019)
at com.sun.jms.util.WorkerThread.run(WorkerThread.java:92)
WARN reaping JMS connection with clientID:
vail1003257390566_6
My best guess is that the reply message is being denied by the
applet for security reasons.
Since I'm running appletviewer for a class on my local machine
I think communication between applet and machine should be fine.
Also I don't think I should be specifying a properties file on the command line
to the appletviewer and I'd like to get away from this but I'm not sure of what
PARAM values I need to specify to do so. (I haven't written many applets before)
appletviewer -J-Djava.security.policy=launch.policy -J-Djms.properties=$J2EE_HOME/config/jms_client.properties Applet.html
Here is the code I've been trying to get working.
To run as a client I just comment out //extends JApplet
comment the ///hash.put(Context.APPLET, this) line and uncomment main.
The exact output I get from running the applet is:
appletviewer -J-Djava.security.policy=launch.policy -J-Djms.properties=$J2EE_HOME/config/jms_client.properties Applet.html
Ignoring com/sun/jms/util/jms.properties due to exception java.lang.NullPointerException
Java(TM) Message Service 1.0.2 Reference Implementation (build b13)
Sending a test message
Sending a test message
Sending a test message
sendMessage exception: javax.jms.JMSException: No Connection found for pclientId:
vail1003266299333_15, connectionId 1
javax.jms.JMSException: No Connection found for pclientId:
vail1003266299333_15, connectionId 1
<<no stack trace available>>
Sending a test message
sendMessage exception: javax.jms.JMSException: No Connection found for pclientId:
vail1003266299333_15, connectionId 1
javax.jms.JMSException: No Connection found for pclientId:
vail1003266299333_15, connectionId 1
<<no stack trace available>>
Sending a test message
sendMessage exception: javax.jms.JMSException: No Connection found for pclientId:
vail1003266299333_15, connectionId 1
javax.jms.JMSException: No Connection found for pclientId:
vail1003266299333_15, connectionId 1
<<no stack trace available>>
^C
If anyone can give me an idea on what I'm doing wrong it would really make my day.
Thanks - Sam Beroz
*/
import java.awt.*;
import javax.swing.*;
import javax.jms.*;
import javax.naming.*;
import java.io.*;
import java.util.*;
public class DynamicClient extends JApplet{
private JLabel counter_label = new JLabel();
private Context jndi_context = null;
private TopicConnectionFactory topic_connection_factory = null;
private TopicConnection topic_connection = null;
private TopicSession subscriber_session = null;
private Topic topic = null;
private TopicSubscriber subscriber = null;
private TopicPublisher publisher = null;
private TopicSession publisher_session = null;
private final String DEFAULT_TOPIC = "APPLET_TEST";
/*
public static void main(String argv[]){
DynamicClient client = new DynamicClient();
client.init();
return;
}
*/
public void init(){
createPanel();
createConnections();
try{
for(int i=0; i<10; i++){
Thread.sleep(1000);
System.out.println("Sending a test message");
sendMessage(new String("Hello All!"));
}
}
catch(java.lang.InterruptedException exception){
System.err.println("sendMessage while loop interrupted: " + exception);
exception.printStackTrace();
}
closeConnections();
return;
}
private void createPanel(){
/*
JPanel main_panel = new JPanel();
main_panel.add(counter_label);
Container contentPane = getContentPane();
contentPane.add(main_panel);
*/
return;
}
//System.setProperty("org.omg.CORBA.ORBInitialHost","208.141.45.132");
//System.setProperty("org.omg.CORBA.ORBInitialPort","1050");
/*
Properties prop = new Properties();
prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.sun.enterprise.naming.SerialInitContextFactory");
prop.setProperty(Context.PROVIDER_URL,"iiop:208.141.45.132:1050");
jndi_context = new InitialContext(prop);
*/
private void createConnections(){
try{
Hashtable hash = new Hashtable();
hash.put(Context.APPLET, this);
hash.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.enterprise.naming.SerialInitContextFactory");
//hash.put(Context.PROVIDER_URL,"iiop:208.141.45.132:1050");
jndi_context = new InitialContext(hash);
}
catch(NamingException exception){
System.err.println("Could not create JNDI context: " + exception);
exception.printStackTrace();
die();
}
try{
topic_connection_factory = (TopicConnectionFactory)
jndi_context.lookup("TopicConnectionFactory");
topic = (Topic) jndi_context.lookup(DEFAULT_TOPIC);
}
catch(NamingException exception){
System.err.println("JNDI lookup failed: " + exception);
exception.printStackTrace();
die();
}
try{
topic_connection =
topic_connection_factory.createTopicConnection();
subscriber_session =
topic_connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
publisher_session =
topic_connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
//subscriber not working, no messages get received!
subscriber = subscriber_session.createSubscriber(topic);
subscriber.setMessageListener(new ClientListener());
publisher = publisher_session.createPublisher(topic);
topic_connection.start();
}
catch(javax.jms.JMSException exception){
System.err.println("Could not create publisher/subscriber: " + exception);
exception.printStackTrace();
die();
}
return;
}
private void die(){
closeConnections();
System.exit(1);
}
private void closeConnections(){
try{
if(null != topic_connection)
topic_connection.close();
}
catch(JMSException exception){
System.err.println("Exception closing connection: " + exception);
exception.printStackTrace();
}
return;
}
public void sendMessage(Serializable data){
try{
javax.jms.ObjectMessage message = publisher_session.createObjectMessage();
message.setObject(data);
publisher.publish(message);
}
catch(JMSException exception){
System.out.println("sendMessage exception: " + exception);
exception.printStackTrace();
}
return;
}
class ClientListener implements MessageListener{
private int message_counter = 0;
public void onMessage(Message message){
System.out.println("Received new message!");
if(message instanceof ObjectMessage)
{
message_counter++;
counter_label.setText("" + message_counter);
}
return;
}
} // End ClientListener class
} // End DynamicClient class