Announcement Announcement Module
Collapse
No announcement yet.
TCP Socket Server Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • TCP Socket Server

    Does anyone have experience running a TCP socket server from a grails application? - I have to interface with some external software and this seems like the easiest solution if I can find out a way to still get dependency injection for GORM access. Otherwise this could run in a separate process and communicate with my Grails app over JMS, but I'd prefer to package them all together for ease of maintenance, updating, etc.

  • #2
    I think your best option is the Background Thread plugin. This will allow you to spawn a background thread to run the TCP socket on while handling the Hibernate session for you.

    Comment


    • #3
      That looks like it might work, I am testing it in Bootstrap and I am able to access domain classes through GORM in the thread it spawns, but I am not able to declare a different service of mine either in the closure that is ran in a new thread or in Bootstrap.groovy itself.

      Is it possible to declare services and have them injected into that closure, or to look them up from the context?

      Comment


      • #4
        You should be able to inject services into BootStrap:
        Code:
        class BootStrap {
            def myService
            def bgThreadManager
        
            def init = { servletContext ->
                bgThreadManager.execute("Doing something in a background thread.") {
                    println myService.output()
                }
            }
        }

        Comment


        • #5
          That seems to work for other services of mine, but the service I'm trying to use needs to access the sessionFactory to clean up after doing a lot of DB operations and I think the DI for that may not be done by the time Bootstrap runs - I get a ton of errors and the service isn't injected to Bootstrap successfully.

          This is the beginning of my service:

          Code:
          class TextResultsImportService implements InitializingBean
          {
          	def grailsApplication
          	def setting
          	def sessionFactory
          	def propertyInstanceMap = org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP
          	
          	void afterPropertiesSet()
          	{
          		this.setting = grailsApplication.config.setting
          	}

          Comment


          • #6
            What error(s) are you seeing?

            Comment


            • #7
              This is the output I get from just declaring my service and the background thread service in Bootstrap it isn't necessary to access it from the closure to cause this stacktrace and kill the app server.

              Code:
              Running Grails application..
              loading security config ...
              13:13:58,148 ERROR GrailsContextLoader:75 - Error executing bootstraps: Error creating bean with name 'textResultsImportService': Cannot create inner bean '(inner bean)' while setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rankingService': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
              org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'textResultsImportService': Cannot create inner bean '(inner bean)' while setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rankingService': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
              	at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:164)
              	at grails.web.container.EmbeddableServer$start.call(Unknown Source)
              	at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:159)
              	at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy)
              	at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:282)
              	at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy)
              	at _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:150)
              	at _GrailsRun_groovy$_run_closure5.call(_GrailsRun_groovy)
              	at _GrailsRun_groovy.runInline(_GrailsRun_groovy:116)
              	at _GrailsRun_groovy.this$4$runInline(_GrailsRun_groovy)
              	at _GrailsRun_groovy$_run_closure1.doCall(_GrailsRun_groovy:59)
              	at RunApp$_run_closure1.doCall(RunApp.groovy:33)
              	at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
              	at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
              	at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
              	at gant.Gant.withBuildListeners(Gant.groovy:427)
              	at gant.Gant.this$2$withBuildListeners(Gant.groovy)
              	at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
              	at gant.Gant.dispatch(Gant.groovy:415)
              	at gant.Gant.this$2$dispatch(Gant.groovy)
              	at gant.Gant.invokeMethod(Gant.groovy)
              	at gant.Gant.executeTargets(Gant.groovy:590)
              	at gant.Gant.executeTargets(Gant.groovy:589)
              Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rankingService': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
              	... 23 more
              Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rankingService': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
              	... 23 more
              Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
              	... 23 more
              Application context shutting down...
              Application context shutdown.

              Comment


              • #8
                Your service depends on rankingService, which is request scope (see 2nd to last cause). There is no request during BootStrap.init() so bean creation fails. Basically, you won't be able to use textResultsImportService from a background thread while that dependency on a request-scoped bean is removed.
                Last edited by pledbrook; Jul 15th, 2010, 02:14 AM.

                Comment


                • #9
                  Perfect! RankingService didn't need to be request-scoped, once I changed that to singleton the app started up fine. Thanks much.

                  Comment

                  Working...
                  X