payara / Payara

Payara Server is an open source middleware platform that supports reliable and secure deployments of Java EE (Jakarta EE) and MicroProfile applications in any environment: on premise, in the cloud or hybrid.
http://www.payara.fish
Other
884 stars 307 forks source link

Get port in runtime via code #1191

Closed phillip-kruger closed 6 years ago

phillip-kruger commented 7 years ago

Hi all. This is not a issue or bug, but rather a question. Apologies if this is not the place to ask questions.

I am looking for a way to get the port number that the server is running on via code. I am using payara micro, and either using --autoBindHttp or passing the port as a startup parameter, is there any way that I can get the port via code ? Maybe a jndi lookup or something ? Any help or tips appreciated. Thanks

smillidge commented 7 years ago

There's a number of ways but the code below should do it in 164. Note the api around this code is moving a little.

List<Integer> ports = PayaraMicro.getInstance().getRuntime().getLocalDescriptor().getHttpPorts()

You can also find the ports etc of all other micro instances in the cluster using;

for (InstanceDescriptor id : PayaraMicro.getInstance().getRuntime().getClusteredPayaras()) {
   id.getHttpPorts();
}
phillip-kruger commented 7 years ago

Thanks! This was exactly what I was looking for. Thanks for the prompt reply!

phillip-kruger commented 7 years ago

Hi Steve. Upgrading to 4.1.1.171.1 I get this exception on the code to get the ports:

Caused by: java.lang.Exception: java.lang.IncompatibleClassChangeError: Found class fish.payara.micro.PayaraMicroRuntime, but interface was expected
        at com.sun.ejb.containers.interceptors.CallbackInvocationContext.proceed(CallbackInvocationContext.java:209)
        at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:73)
        at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.sun.ejb.containers.interceptors.CallbackInterceptor.intercept(InterceptorManager.java:998)
        at com.sun.ejb.containers.interceptors.CallbackChainImpl.invokeNext(CallbackChainImpl.java:72)
        at com.sun.ejb.containers.interceptors.CallbackInvocationContext.proceed(CallbackInvocationContext.java:205)
        at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
        at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.init(SystemInterceptorProxy.java:125)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.sun.ejb.containers.interceptors.CallbackInterceptor.intercept(InterceptorManager.java:998)
        at com.sun.ejb.containers.interceptors.CallbackChainImpl.invokeNext(CallbackChainImpl.java:72)
        at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:417)
        at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:380)
        at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:1962)
        at com.sun.ejb.containers.AbstractSingletonContainer.createSingletonEJB(AbstractSingletonContainer.java:468)

Do you know about this ?

Thanks

mikecroft commented 7 years ago

Have you updated your pom.xml to use the new API? I've got an example in one of my repos - here's a link to the diff: https://github.com/mikecroft/PiyaraMicroDemo/commit/f2eafeca51347994bc2296133e5dff766e52c7a9

The dependency should look like this:

        <dependency>
            <groupId>fish.payara.api</groupId>
            <artifactId>payara-api</artifactId>
            <version>4.1.1.171.1</version>
            <scope>provided</scope>
        </dependency>
phillip-kruger commented 7 years ago

:+1: Yes I did. However fish.payara.micro.PayaraMicro is not in the API. So I still need

<dependency>
      <groupId>fish.payara.extras</groupId>
      <artifactId>payara-micro</artifactId>
      <scope>provided</scope>
</dependency>

The code that I use to get the HTTP port:

fish.payara.micro.PayaraMicro instance = fish.payara.micro.PayaraMicro.getInstance();
fish.payara.micro.PayaraMicroRuntime runtime = instance.getRuntime();
fish.payara.micro.data.InstanceDescriptor localDescriptor = runtime.getLocalDescriptor();

On runtime.getLocalDescriptor I get the above exception.

Any advice ?

Thanks

phillip-kruger commented 7 years ago

B.t.w. - what would be great is to make the ports (http and https) and the hostname available via some JNDI resource.

@Resource(lookup="jndi/httpPort")
private Integer httpPort;

Something like that ?

phillip-kruger commented 7 years ago

Some more info. If I try to build a uber jar using maven I get an error. Here my maven part:

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.5.0</version>
                <dependencies>
                    <dependency>
                        <groupId>fish.payara.extras</groupId>
                        <artifactId>payara-micro</artifactId>
                        <version>${payara.version}</version>
                    </dependency>

                </dependencies>
                <executions>
                    <execution>
                        <id>payara-uber-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>fish.payara.micro.PayaraMicro</mainClass>
                            <arguments>
                                <argument>--deploy</argument>
                                <argument>${basedir}/target/myapp.war</argument>
                                <argument>--domainConfig</argument>
                                <argument>${basedir}/target/domain.xml</argument>
                                <argument>--autoBindHttp</argument>
                                <argument>--autoBindSsl</argument>
                                <argument>--disablePhoneHome</argument>
                                <argument>--outputUberJar</argument>
                                <argument>${basedir}/target/${project.build.finalName}.jar</argument>
                            </arguments>
                            <includeProjectDependencies>false</includeProjectDependencies>
                            <includePluginDependencies>true</includePluginDependencies>

                            <executableDependency>
                                <groupId>fish.payara.extras</groupId>
                                <artifactId>payara-micro</artifactId>
                            </executableDependency>

                        </configuration>
                    </execution>
                </executions>
            </plugin>

Then the error on mvn clean install

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:294)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: fish/payara/micro/boot/PayaraMicroBoot
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
OndroMih commented 7 years ago

Hi, @phillip-kruger , we already know that running Payara Micro v4.1.1.171 and newer from maven doesn't work well because of classpath issues and we'll try to fix it. It's related to the changes in the structure of the JAR file we introduced this year and it's not an easy fix.

To package an uber JAR, can you try our snapshot maven plugin? It's not yet released into Maven central so you'll need to build it from source and install into your local repository with mvn install, but it should do what you need.

smillidge commented 7 years ago

Maven plugin is now on maven central https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22fish.payara.maven.plugins%22

smillidge commented 7 years ago

In 173 when released this will be possible via the Microprofile config api using code like;

@Inject
@ConfigProperty(name="payara.instance.http.port")
int port;
renis1235 commented 1 year ago

Thank you! I was looking for this.

Is this documented anywhere? This is the only place I could find how to retrieve the port.