[jboss-user] [Beginners Corner] - Re: java.lang.ClassCastException: org.jnp.interfaces.NamingC

tamscot do-not-reply at jboss.com
Mon Nov 26 18:47:50 EST 2007


Okay here goes. This is a message driven ejb. The user enters a string which represents an identification. This is done from a html form and text input. The idea is the string is used as an id for the creation of a new object.

Here is the html

  | <form action="CreateClubAction" method="GET">
  |           <table>
  | 		<tr><th>Club ID</th><th><input type="text" name="clubId" value=""/></th></tr>
  | 		<tr><td colspan="2"><input type="submit" name="add" value="add"/></td></tr>
  | 	</table>
  | </form>
  | 

The url "CreateClubAction" is a servlet which creates an "in memory copy of data required to initialize the application. It then sends a message, the string entered into the html page and replies to standout the string entered whilst displaying a jsp page with a copy of the in memory data.

here is some of CreateClubAction.java, the servlet.

  | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  | 		try{
  | 			String clubId = request.getParameter("clubId");
  | 			sendMessage(clubId);
  | 			forward(request, response);
  | 		}catch(Exception e){
  | 			e.printStackTrace();
  | 		}
  | 	}
  | 
  | private void sendMessage(String clubId) throws 
  | 				NamingException, JMSException {
  | 		QueueConnection qConnection = 
  | 			AsyncJujitsuFacadeUtil.getQueueConnection();
  | 		Queue queue = AsyncJujitsuFacadeUtil.getQueue();
  | 		QueueSession qSession = 
  | 			qConnection.createQueueSession(false, SESSIONTYPE);
  | 		QueueSender qSender = qSession.createSender(queue);
  | 		
  | 		Club aClub = new Club();
  | 		aClub.setId(clubId);
  | 		
  | 		ObjectMessage objectMessage = qSession.createObjectMessage(aClub);
  | 		qSender.send(objectMessage);
  | 		qSession.close();
  | 		qConnection.close();
  | 		
  | 	}
  | 
  | private void forward(HttpServletRequest request, HttpServletResponse response) 
  | 			throws ServletException, IOException {
  | 		this.getServletContext().getRequestDispatcher("/students.jsp")
  | 				.forward(request, response);
  | 		
  | 	} 
  | 

Where it is falling down is on the call to sendMessage() above. Which equates to the line in the xdoclet created class AsyncJujitsuFacadeUtil.java shown below.

  |    public static javax.jms.QueueConnection getQueueConnection() throws javax.naming.NamingException, javax.jms.JMSException
  |    {
  |       if (cachedConnectionFactory == null) {
  |          // Obtain initial context
  |          javax.naming.InitialContext initialContext = new javax.naming.InitialContext();
  |          try {
  |             java.lang.Object objRef = initialContext.lookup(CONNECTION_FACTORY_JNDI_NAME);
  |             cachedConnectionFactory = (javax.jms.QueueConnectionFactory) objRef;
  |          } finally {
  |             initialContext.close();
  |          }
  |       }
  |       return cachedConnectionFactory.createQueueConnection();
  |    }
  | 

The line below is where the exception points to...


  |             cachedConnectionFactory = (javax.jms.QueueConnectionFactory) objRef;
  | 

The bean class is shown below...


  | public class AsyncJujitsuFacadeBean implements javax.ejb.MessageDrivenBean,
  | 		javax.jms.MessageListener {
  | 
  | 	/**
  | 	 * 
  | 	 */
  | 	private static final long serialVersionUID = 1L;
  | 	/** 
  | 	 * <!-- begin-user-doc -->
  | 	 * <!-- end-user-doc -->
  | 	 * The context for the message-driven bean, set by the EJB container. 
  | 	 * @generated
  | 	 */
  | 	private javax.ejb.MessageDrivenContext messageContext = null;
  | 	private JujitsuFacade jujitsuFacade;
  | 
  | 	/** 
  | 	 * Required method for container to set context.
  | 	 * @generated 
  | 	 */
  | 	public void setMessageDrivenContext(
  | 			javax.ejb.MessageDrivenContext messageContext)
  | 			throws javax.ejb.EJBException {
  | 		this.messageContext = messageContext;
  | 	}
  | 
  | 	/** 
  | 	 * Required creation method for message-driven beans. 
  | 	 *
  | 	 * <!-- begin-user-doc -->
  | 	 * <!-- end-user-doc -->
  | 	 *
  | 	 * <!-- begin-xdoclet-definition -->
  | 	 * @ejb.create-method 
  | 	 * <!-- end-xdoclet-definition -->
  | 	 * @generated
  | 	 */
  | 	public void ejbCreate() {
  | 		//no specific action required for message-driven beans 
  | 		jujitsuFacade = ClubFacade.getJujitsuFacade();
  | 	}
  | 
  | 	/** 
  | 	 * Required removal method for message-driven beans. 
  | 	 * <!-- begin-user-doc -->
  | 	 * <!-- end-user-doc -->
  | 	 * @generated
  | 	 */
  | 	public void ejbRemove() {
  | 		messageContext = null;
  | 	}
  | 
  | 	/** 
  | 	 * This method implements the business logic for the EJB. 
  | 	 * 
  | 	 * <p>Make sure that the business logic accounts for asynchronous message processing. 
  | 	 * For example, it cannot be assumed that the EJB receives messages in the order they were 
  | 	 * sent by the client. Instance pooling within the container means that messages are not 
  | 	 * received or processed in a sequential order, although individual onMessage() calls to 
  | 	 * a given message-driven bean instance are serialized. 
  | 	 * 
  | 	 * <p>The <code>onMessage()</code> method is required, and must take a single parameter 
  | 	 * of type javax.jms.Message. The throws clause (if used) must not include an application 
  | 	 * exception. Must not be declared as final or static. 
  | 	 *
  | 	 * <!-- begin-user-doc -->
  | 	 * <!-- end-user-doc -->
  | 	 * @generated
  | 	 */
  | 	public void onMessage(javax.jms.Message message) {
  | 		// begin-user-code
  | 		try{
  | 			Club club = (Club)((ObjectMessage) message).getObject();
  | 			jujitsuFacade.createClub(club);
  | 			System.out.println("A New Club: " + club.getId());
  | 		}catch(JMSException e){
  | 			e.printStackTrace();
  | 		}
  | 		// end-user-code
  | 	}
  | 
  | 	/**
  | 	 * 
  | 	 */
  | 	public AsyncJujitsuFacadeBean() {
  | 		// TODO Auto-generated constructor stub
  | 	}
  | }
  | 

Hope someone can understand whats going wrong here as I can't.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4107900#4107900

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4107900



More information about the jboss-user mailing list