Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Cant retrieve Bean by it's id via BeanFactory

  1. #1

    Default Cant retrieve Bean by it's id via BeanFactory

    Hi,

    I am registering Bean manually to the container this way:

    Code:
    public class AppContextExtendingBean implements ApplicationContextAware{
    
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
    
            AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
    
    
       
            addBean(beanFactory);
    
        }
    
        // create the object manually and then inject it into the spring context
        private void addBean(AutowireCapableBeanFactory beanFactory){
            MyObject myObject=new MyObject("foo","phleem");
            myObject.setBar("baz");
            beanFactory.autowireBean(myObject);
            beanFactory.initializeBean(myObject, "bean2");
        }

    Now this bean is alive and working.

    Now i am trying to destroy this bean manuall this way:

    Code:
    AbstractBeanFactory factory = (AbstractBeanFactory) beanFactory;
    			factory.destroyScopedBean("bean2");
    and I get this error



    Code:
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'bean2' is defined
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.destroyScopedBean(AbstractBeanFactory.java:977)
    	at com.fixgw.sessions.SessionFactory.removeSessionByIntSessionId(SessionFactory.java:107)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    	at javax.management.modelmbean.RequiredModelMBean.invokeMethod(Unknown Source)
    	at javax.management.modelmbean.RequiredModelMBean.invoke(Unknown Source)
    	at org.springframework.jmx.export.SpringModelMBean.invoke(SpringModelMBean.java:90)
    	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(Unknown Source)
    	at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(Unknown Source)
    	at mx4j.tools.adaptor.http.InvokeOperationCommandProcessor.executeRequest(InvokeOperationCommandProcessor.java:164)
    	at mx4j.tools.adaptor.http.HttpAdaptor$HttpClient.run(HttpAdaptor.java:980)

    Why the container doesn't find bean with name bean2?

    thanks,
    ray.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Because it isn't in control of the bean. For bean desctruction it needs to know what to do, this is retrieved from the BeanDefinition and as you are in control of that bean that isn't available...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Could you provider another solution how could I destroy that bean programmaticly?

    or how should I add it manually in first place in a way I could "put my hands on it" in a later stage?

    thanks.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    I have just one question why? Why do you want a bean instance you create to be managed by spring?! I don't get that, what is your usecase...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    Okie Ill tell you why.

    I have topic.

    this topic has subscribers.

    now those subscribers will be registered to this topic on the fly.

    The same goes for un-registration.

    I want each subscriber to be a spring bean coz I want it to be managed by the container and have all services on it(AOP, auto wiring, etc..) as long as it's subscribed. when it's destroyed I want it to be off the container.

    so I found a way to register subscribers dynamiclly but cant unregister(destroy) them on demand (as you already know)

    any other soltuion to my usecase will be acceptable !!

    this is how a subscriber looks like:


    Code:
    public class Subscriber implements MessageListener
    																											
    {
    	protected FixSessionDTO fixSessionDTO;
    
    
    	protected static Logger log = Logger.getLogger(AbstractSessionBean.class);;
    
    
    
    
    	@Autowired
    	ConnectionFactory connectionFactory;
    
    
    
    
    	DefaultMessageListenerContainer messageListeners;
    
    
    
    
    	private Topic topicDestination;
    
    
    
    
    	public Topic getTopicDestination()
    	{
    		return topicDestination;
    	}
    
    
    
    
    	public void setTopicDestination(Topic topicDestination)
    	{
    		this.topicDestination = topicDestination;
    	}
    
    
    	public Subscriber (FixSessionDTO fixSessionDTO, Topic topicDestination)
    	{
    		super();
    		this.fixSessionDTO = fixSessionDTO;
    		this.topicDestination = topicDestination;
    	}
    
    
    	public FixSessionDTO getFixSessionDTO()
    	{
    		return fixSessionDTO;
    	}
    
    
    
    
    	public void setFixSessionDTO(FixSessionDTO fixSessionDTO)
    	{
    		this.fixSessionDTO = fixSessionDTO;
    	}
    
    
    
    
    	@PostConstruct
    	public void init()
    	{
    
    
    
    
    		try
    		{
    			System.out.println("AbstractSessionBean.init()." + fixSessionDTO.toString());
    			messageListeners = new DefaultMessageListenerContainer();
    			CachingConnectionFactory cacheFactory = new CachingConnectionFactory();
    			cacheFactory.setTargetConnectionFactory(connectionFactory);
    			cacheFactory.setReconnectOnException(true);
    			cacheFactory.setExceptionListener(new ExceptionListener()
    			{
    
    
    
    
    				public void onException(JMSException e)
    				{
    					log.error("Error", e);
    				}
    			});
    			cacheFactory.setSessionCacheSize(1);
    
    
    
    
    			messageListeners.setConnectionFactory(connectionFactory);
    			messageListeners.setDestinationName(topicDestination.getTopicName());
    			messageListeners.setMaxConcurrentConsumers(1);
    			messageListeners.setSessionAcknowledgeModeName("AUTO_ACKNOWLEDGE");
    			messageListeners.setMessageListener(this);
    			messageListeners.setPubSubDomain(true);
    			messageListeners.setAcceptMessagesWhileStopping(false);
    			messageListeners.setConcurrentConsumers(1);
    			messageListeners.setSessionTransacted(false);
    			messageListeners.setReceiveTimeout(1000);
    			messageListeners.setMaxMessagesPerTask(300);
    			messageListeners.initialize();
    			messageListeners.start();
    
    
    
    
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    
    
           @Override
    	public void onMessage(Message message)
    	{
    		
    	  // do something with the msg
    	}
    
    
    
    
    
    	@PreDestroy
    	public void destroy()
    	{
    		try
    		{
    			log.debug("Destroying MDB. details=" + fixSessionDTO.toString());
    			if (messageListeners != null)
    			{
    				messageListeners.stop();
    				messageListeners.shutdown();
    				messageListeners.destroy();
    			}
    		}
    		catch (Exception e)
    		{
    			log.error("Couldnt destroy MDB. details=" + fixSessionDTO.toString(), e);
    		}
    
    
    
    
    	}
    
    	public void stop()
    	{
    		messageListeners.stop();
    	}
    
    
    
    
    	public void start()
    	{
    		messageListeners.start();
    	}
    }

    connectionFactory being wired from the configuration file. but the property class FixSessionDTO I must set on the subscriber on the fly also.


    ray
    Last edited by ray.frid; Jul 16th, 2012 at 04:21 AM.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    The only thing you get is autowiring and that is something you can do without registering your bean as a spring bean. However I would probably use Spring Integration that gives an easy way to (un)subscribe to channels.

    Code:
    private SubscribableChannel channel;
    
    public void subscribe() {
    
      MessageHandler handler = new MessageHandler();
      channel.subscribe(handler);
    
    }
    
    public void unsubscribe() {
    
     channel.unsubscribe(handler);
    }
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Default

    I guess now that I miss lots of information.

    So you saying I should download the Spring integration libs(which contains the SubscribableChannel class)

    and each listener of mine suppose to implement that SubscribableChannel. in this way Ill be able to send a message to each spring bean a message like stop,start etc.. but still how will I dispose those "channels" from the container bean when I wont need to use them anymore? (thanks for your patience)

    ray.

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    No.. You need a MessageHandler not a channel!!! Also you shouldn't make it a container managed bean but control it yourself (you are also constructing it yourself!) you can still do autowiring if you need (you could even make it a prototype scoped bean to let the container act as a factory for your bean). After unsubscribing it and programmed correctly it will be garbage collected.

    No need to make things more complex as needed...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9

    Default

    A bit more patience and I am getting there.. was struggling with this for days now.


    1. So if you think I shouldnt make it as a container managed bean. why do I need the MessageHandler for?

    I am thinking about having a hashmap table which will hold all my pojos.

    and if for example I need to "speak" with some pojo I will just retrive it from the hashmap and execute a method.

    2. Could you please detail this: (you could even make it a prototype scoped bean to let the container act as a factory for your bean)



    thanks.

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    1. So if you think I shouldnt make it as a container managed bean. why do I need the MessageHandler for?
    You need something that handles the message doesn't it?! Nothing/thin air//dev/null/ cannot handle a message. You need an object to handle the message. But not the whole world has to be managed by the spring container! As mentioned yuo can still have the dependencies injected without making it a container managed bean (I suggest a read of the javadoc for the ApplicationContext/BeanFactory.

    2. Could you please detail this: (you could even make it a prototype scoped bean to let the container act as a factory for your bean)
    I suggest a read of the reference guide on scopes. You can simply add it to your applicationcontext and when you need an instance call getBean("your-id) to get a fresh instance which is then under your control (saves you the step of calling autowire on the BeanFactory and you can get more leverage of the Spring AOP stuff).

    I suggest you learn the framework and how it can help you instead of trying to hammer your solution in/around the framework (like the Map idea ).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •